Written by Allen Wyatt (last updated September 26, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365
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.
The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!
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 MoreWhen creating a macro, you may need to determine the names of the bookmarks in the document. You can do this using the ...
Discover MoreBookmarks are a great boon in developing and working with documents--"until someone deletes them. When it comes to ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments