Here's the code I'm presently using. It's a bit spartan, but it gets
the job done. Two macros. One called from a macro that had just
inserted a caption, and the other called from a macro that had just
inserted a cross reference.
I've been working a lot with VBA in excel since 1995, but Word VBA is
entirely new to me. If you see anything I ought to be doing
differently, please let me know.
Cheers,
Brian
Sub mrfAddBookmark()
'right after creating a caption that has a label followed by a caption
number
'define a bookmark that contains just the caption number
'the bookmark ID will be mrf########## where the digits are from the
current time of day
'it doesn't matter if the caption number includes a chapter number
(maybe), and any separator is okay
'upon entering this routine the caption number should be selected
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="mrf" &
Int((Now - Int(Now)) * 10 ^ 9)
Selection.MoveRight Unit:=wdCharacter, Count:=2
End Sub
Sub mrfChangeCrossReference()
'this operates on a REF cross-reference field for a caption
'The bookmark ID in the field is one of Word's hidden bookmarks that
includes the caption label
'There should be one of my own bookmarks inside that one that excludes
the label
'This routine changes the bookmark ID in the cross-reference from
Word's to mine
Dim s$, s1$, s2$, s3$, rng As Range
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Set rng = Selection.Fields(1).Code
s = Trim(rng.Text)
s1 = Mid(s, 1, InStr(s, " "))
s = Mid(s, Len(s1) + 1)
s2 = Mid(s, 1, InStr(s, " ") - 1)
s = Mid(s, Len(s2) + 1)
s2 = ActiveDocument.Bookmarks(s2).Range.Bookmarks(1).Name
s = " " & s1 & s2 & s3 & " "
rng.Text = s
Selection.Fields(1).Update
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub