Bookmarks appear in {PAGEREF} fields as "_Ref<number>", not as "<name>". Why?

S

Stephen

Hello, everyone.

I've been sent a document to process by my employers that contains a
number of Word bookmarks and {PAGEREF} references to them. The usual
form for these is {PAGEREF <name>}, as {PAGEREF AutoSelection} or
whatever. Somehow, though, in the course of being processed, the names
in the {PAGEREF} fields have disappeared and been replaced by numbers.
They now look like this:

{PAGEREF _Ref500736802 \h}

Now, the "\h" bit is easy - the Word help file explains that it sets
up a hyperlink to the paragraph which has the given bookmark. However,
there is NO bookmark called "_Ref500736802" in the document - all the
bookmarks have human-readable names such as CrOview, Referral1,
SupOview and LogOnCom. The only reference to "_REF" I can find in the
Word help file is in the "Troubleshoot cross-references" section,
where one help page has the heading "I see {REF _Ref249586 \*
MERGEFORMAT} instead of the cross-reference." This, we are told,
merely means that we should hide field codes. It doesn't tell us where
the _Ref code comes from in the first place.

My employers want me to go through the document changing all the
{PAGEREF _Ref<code>} fields to {PAGEREF <bookmark name>} fields. Until
I know how the _Ref format of a cross-reference can be correlated with
a bookmark name, I can't do anything about it. Does anyone know how to
do this?

Thank you!
 
J

Jay Freedman

Hi, Stephen,

This job calls for a fairly nasty bit of code. If the comments aren't enough
to explain how it works, post back. :)

Sub FixBookmarkRefs()
' Where a PAGEREF cross-reference uses
' a hidden bookmark like _Ref123456 to
' refer to a range that also has a
' non-hidden bookmark applied to it,
' change the cross-reference to use the
' non-hidden bookmark.

Dim oFld As Field
Dim oBMk As Bookmark
Dim RefRange As Range
Dim FldCode As String, RefName As String
Dim RefPos As Integer

With ActiveDocument
For Each oFld In .Fields
If oFld.Type = wdFieldPageRef Then

' get the field code and check it
' for a hidden bookmark name
FldCode = oFld.Code
RefPos = InStr(FldCode, "_Ref")
If RefPos Then

' isolate the bookmark name
RefName = Right(FldCode, _
Len(FldCode) - RefPos + 1)
RefName = Left(RefName, _
InStr(RefName, " ") - 1)

' get the range of the hidden bookmark
' (available only if ShowHidden=True)
.Bookmarks.ShowHidden = True
Set RefRange = .Bookmarks(RefName).Range

' now search through the non-hidden
' bookmarks for one that is contained
' within RefRange
.Bookmarks.ShowHidden = False
For Each oBMk In .Bookmarks
If RefRange.InRange(oBMk.Range) Then

' found it, so change the code of
' the cross-reference field
FldCode = _
Replace(FldCode, RefName, oBMk.Name)
oFld.Code.Text = FldCode

' there can be only one, so don't
' bother to search the rest of the doc
Exit For
End If
Next oBMk
End If
End If
Next oFld

.Fields.Update
End With
End Sub
 
M

martinique

Have you tried displaying hidden bookmarks? (Insert > Bookmarks, Hidden
Bookmarks checkbox)

Any bookmark that begins with an underscore is 'hidden'. The bookmarks you
are seeing are the type created automatically by Word when you insert a
cross-reference to a heading within the document.
 
S

Stephen

Thanks to both of you for answering; especially to Jay for his
detailed algorithm! It works, but fortunately - otherwise I could
hardly justify being paid! - it requires quite a lot more adding to it
before it will do everything my employers want. It is a huge step
forward, though: I had no idea that hidden bookmarks even existed, or
of the InRange function. Finally, the MVP site is also immensely
useful and I have commended it to my boss. Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top