Finding Cross-References to Specific Bookmarks

Written by Allen Wyatt (last updated August 8, 2020)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


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 Microsoft 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

Turning the Legend On and Off

When you create a chart in Excel, the program may automatically add a legend that explains the contents of the chart. In ...

Discover More

Setting User Information

Need to change the information that Word stores about you? Here's how to find the info.

Discover More

Changing the Background Color for a Comment

Comments are a great way to document your worksheets. Excel provides you the tools you need in order to format your ...

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)

Setting a VBA Variable from a Bookmark

Bookmarks are quite helpful in a document. You may want to transfer the contents of a bookmark into a macro variable in ...

Discover More

Making Bookmarks Bold

Do you want an easy way to see all the bookmarks in your document? Word provides a way to make them visible, or you can ...

Discover More

Printing a Bookmark List

Need to know what bookmarks are defined in a document? Here's a macro that creates a list of all your bookmarks so that ...

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}] (all 7 characters, in the sequence shown) 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 four less than 7?

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.

Videos
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.