Finding Cross-References to Specific Bookmarks

by Allen Wyatt
(last updated August 8, 2020)

1

James bookmarks items in his documents for the sake of cross-referencing. Sometimes, in the process of editing, he may need to delete something that he previously bookmarked. James wonders if there is a way to find out if there are any cross-references to the text (and bookmark) that he is thinking of deleting.

If you only need to perform this task once in a while, you can do it manually. How you approach it, though, is going to depend on how you created the cross-reference. You see, when you insert a cross-reference, Word displays the Cross-reference dialog box. (See Figure 1.)

Figure 1. The Cross-reference dialog box.

In the dialog box, using the Reference Type drop-down list, you can choose the type of cross-reference you want to create. Each type of reference utilizes a different variation of the REF field to insert the actual cross-reference. For instance, if you insert a cross-reference to a bookmark, then the field that is inserted looks like this:

{ REF MyBookmark \h }

The "MyBookmark" part is the bookmark name you are cross-referencing to. The parameter (in this case \h) is controlled by the Insert Reference To drop-down list in the Cross-reference dialog box. If you, instead, insert a cross-reference to a heading in the document, it will look similar to this:

{ REF _Ref47603047 \h }

The "_Ref47603047" portion of this field code is a system-generated bookmark that is hidden. It refers to the heading you selected for your cross-reference. You can see these hidden bookmarks if you display the Bookmark dialog box and click the Hidden Bookmarks check box at the bottom of the dialog box.

In this tip, because James specifically asked to find cross-references to bookmarked text, I'm going to assume that when he created the cross-reference, he did so by choosing Bookmark in the Insert Reference To drop-down list of the Cross-reference dialog box. This actually makes it a bit easier, as well, to find if your bookmark is cross-referenced anywhere.

Start by figuring out the name of the bookmark that is in the text that you are considering deleting. In this instance, I'm going to assume it is a name such as MyBookmark. All you need to do is to press Alt+F9, which causes Word to display the field codes in your document rather than the results of the field codes. At this point you can simply search for the bookmark name (MyBookmark, in this case) and you'll be able to find any REF field (remember, REF fields are used for cross-references) that contains the bookmark name. If you don't find the bookmark name, then there is no cross-reference to that bookmark, and you can safely delete the text and the bookmark it contains. When you are all done, press Alt+F9 again to turn off the display of field codes.

If you need to find out whether a bookmark is referenced more than once in a while, or if you want to perform a more complete search than what Find and Replace offers, then you should consider using a macro. The following set of four macros can come in handy in this situation.

Sub IsBookmarkReferenced()
    Dim aStory As Range
    Dim aShape As Shape
    Dim aField As Field
    Dim bkName As String
    Dim bReffed As Boolean

    bReffed = False
    If Selection.Bookmarks.Count > 0 Then
        bkName = Selection.Bookmarks(1).Name
        For Each aStory In ActiveDocument.StoryRanges
            If TestForBookmark(aStory, bkName) Then
                bReffed = True
            Else
                Select Case aStory.StoryType
                    Case wdMainTextStory, wdEvenPagesHeaderStory, _
                      wdPrimaryHeaderStory, wdEvenPagesFooterStory, _
                      wdPrimaryFooterStory, wdFirstPageHeaderStory, _
                      wdFirstPageFooterStory
                        For Each aShape In aStory.ShapeRange
                            If aShape.TextFrame.HasText Then
                                If TestForBookmark(aShape.TextFrame.TextRange, bkName) Then bReffed = True
                            End If
                        Next
                    End Select
                Next aStory
            Endif
        Next aStory
        sTemp = "Bookmark " & bkName & " is "
        If Not bReffed Then sTemp = sTemp & "NOT "
        sTemp = sTemp & "referenced in the document."
    Else
        sTemp = "There is no bookmark in the selected text."
    End If
    MsgBox sTemp
End Sub
Function TestForBookmark(tRange As Range, bkName As String) As Boolean
    Dim aField As Field

    TestForBookmark = True
    For Each aField In tRange.Fields
        Select Case aField.Type
            Case wdFieldRef, wdFieldAsk, wdFieldBarCode, _
              wdFieldGoToButton, wdFieldHyperlink, _
              wdFieldNoteRef, wdFieldPageRef, wdFieldSet
                If BookRef(aField.Code.Text, aField.Type) = bkName Then Exit Function
            Case wdFieldTOC, wdFieldTOA
                If TOCRef(aField.Code.Text) = bkName Then Exit Function
        End Select
    Next aField
    TestForBookmark = False
End Function
Function BookRef(str As String, typeCode As Long) As String
    Dim s As String
    Dim i As Long

    s = Trim(str)
    If s <> "" Then
        i = InStr(s, " ")
        If i > 0 Then s = Trim(Mid(s, i))
        If typeCode = wdFieldHyperlink Then
            If InStr(s, "\l") > 0 Then
                s = Mid(s, InStr(s, """") + 1)
                i = InStr(s, """")
                If i > 1 Then s = Left(s, i - 1)
            Else
                s = ""
            End If
        Else
            i = InStr(s, " ")
            If i > 0 Then s = Trim(Left(s, i))
        End If
    End If
    BookRef = s
End Function
Function TOCRef(str As String) As String
    Dim s As String
    Dim i As Long

    s = Trim(str)
    i = InStr(s, "\b")
    If i = 0 Then
        TOCRef = ""
        Exit Function
    End If
    s = Trim(Mid(s, i + 2))
    i = InStr(s, " ")
    If i > 0 Then s = Left(s, i - 1)
    TOCRef = s
End Function

In order to use the macros, all you need to do is to select the text you are considering deleting and then run the IsBookmarkReferenced macro. It, in turn, utilizes the other three functions to figure out if any bookmark in the selected text is referenced elsewhere. The macro will check fields not only in the main document itself, but also in headers, footers, and text boxes.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (7619) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Office 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Days Left in the Year

Sometimes it is handy to know how many days are left in the current year. This tip provides a quick formula that ...

Discover More

Editing Word's Built-in Commands

Want to configure Word to do just what you want it to? You can even go so far as to change the actual way in which Word ...

Discover More

Using Correct Apostrophes

Word does a pretty good job of figuring out what apostrophes to use around your text. There may be times, however, when ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Removing All Bookmarks

Need to get rid of a lot of bookmarks all at once? Word doesn't provide a way to do it, but you can use the short macro ...

Discover More

Random OLE Bookmarks

Word has an irritating habit of placing unwanted bookmarks in a document that you've been editing for a while. These ...

Discover More

Getting the Names of Defined Bookmarks

When creating a macro, you may need to determine the names of the bookmarks in the document. You can do this using the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 5?

2020-08-08 09:59:56

Dorothy LucyAnn Curling

This is 'almost' the tip I need! I've inserted an extra appendix to a chapter in the book I'm writing. Each chapter has a set of appendices with the titles of each corresponding to the chapter number, so Chapter 2 has appendices 2a, 2b, 2c etc. I need to update each of the cross-references to each of the appendices for this one chapter, but I can't see how to do that.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.