0
votes

In Word I have a list of Bookmarks and want to insert cross references (copy the text and have a hyperlink) to those bookmarks. With macro recording and adjusting parameters I got this which replaces myRange with the text of myBookmark and makes a hyperlink to that bookmark:

myRange.InsertCrossReference _
    ReferenceType:=wdRefTypeBookmark, _
    ReferenceKind:=wdContentText, _
    ReferenceItem:=myBookmark, _
    InsertAsHyperlink:=True

This works fine except for one thing: if I change the style of the text I bookmarked (e.g. make it bold) and update the cross references then the cross references also adopt that style - I'd need to copy only the text in the bookmark, but the style (color, boldness, whatever) should remain the one from the context where the cross reference is (e.g. text should stay italic or blue or whatever).

Changing the style of the cross reference manually isn't of much help - after updating the cross reference field, the style falls back to the one of the bookmark.

I can't believe that there's no option for this, and I don't even see how to get that kind of bookmark/cross reference manually (without VBA). Any hints?

Note: when I do a ClearFormatting to the range before I bookmark it (and set that style back afterwards), then cross references will keep their style even after updating the field (so far so good), unless (sic!) they don't have a style, then they will adopt the changed style of the bookmark - if the bookmark is in plain text, this isn't a problem, but if it is in - say - a table with italic words, I'll adopt this italic format for all cross references that don't have a style yet. So, what's a general solution?

1

1 Answers

2
votes

The answer is to use the * CharFormat switch in the REF field code underlying the cross-reference.

Example cross-reference to the bookmark "test":

REF test \h \* CharFormat

You can see the field code by toggling the view using Alt+F9. Test by adding the switch, then pressing F9 to force the field update.

There's no option for adding this switch in the object model. But you can do it fairly easily by editing the Field.Code.Text property. Example

ActiveDocument.Fields(2).Code.Text = _
     ActiveDocument.Fields(2).Code.Text & " \* CharFormat "