I have quite a large word document (> 400 pages) with lots of cross references to headings. So far, I have always referred to the title of the heading, but now I would like to change that and refer to the page the heading resides on.
I didn’t find a solution to this via the GUI (except manual treatment, of course), so I was looking into writing some VBA. Unfortunately, I have only found a way to list all targets that can be cross referenced (via GetCrossReferenceItems
), but I need a way to access the actual cross reference field.
Can you help me with that? Is a cross reference field the same as a hyperlink?
0m3r
12.2k15 gold badges33 silver badges70 bronze badges
asked Jul 25, 2014 at 8:39
1
Cross-references are fields in a Word document, and can be accessed via the Fields collection (ActiveDocument.Fields). You can loop through them like any other collection and check their types to see if it’s one you want to work on. It looks like cross-references to text are type 3 (wdFieldRef) and cross-references to page numbers are type 37 (wdFieldPageRef). Changing fields can be a little tricky; the following should get you started:
Sub ChangeFields()
Dim objDoc As Document
Dim objFld As Field
Dim sFldStr As String
Dim i As Long, lFldStart As Long
Set objDoc = ActiveDocument
' Loop through fields in the ActiveDocument
For Each objFld In objDoc.Fields
' If the field is a cross-ref, do something to it.
If objFld.Type = wdFieldRef Then
'Make sure the code of the field is visible. You could also just toggle this manually before running the macro.
objFld.ShowCodes = True
'I hate using Selection here, but it's probably the most straightforward way to do this. Select the field, find its start, and then move the cursor over so that it sits right before the 'R' in REF.
objFld.Select
Selection.Collapse wdCollapseStart
Selection.MoveStartUntil "R"
'Type 'PAGE' to turn 'REF' into 'PAGEREF'. This turns a text reference into a page number reference.
Selection.TypeText "PAGE"
'Update the field so the change is reflected in the document.
objFld.Update
objFld.ShowCodes = True
End If
Next objFld
End Sub
answered Jul 25, 2014 at 19:44
ChristinaChristina
1,3491 gold badge11 silver badges22 bronze badges
3
If you are wanting a list of everything for which a cross-reference has been made in a document, there is no such thing.
You can search the document for Ref fields, though.
Every time a cross-reference is created to something like a heading, a hidden bookmark is created. You can look at those in the GoTo dialog but I doubt it will help you.
Here is a screenshot using the Find feature to find REF fields to hidden bookmarks.
Here is the Insert bookmark dialog that lets you see hidden bookmarks in the list by checking the box.
Here is the GoTo dialog that lets you go to the bookmarks. (Ctrl+G)
This is in no way as simple as getting a list. As you can see, the names attached to the bookmarks are semi-random. In this case, the cross-references to the headings were made first and to the tables, second. No distinction is made between types of cross-reference or target in the name of the bookmark.
Of course, if the only cross-references are to tables, it simplifies things.
The Find feature, with field codes displayed, finds REF fields and gives you a count.
By going to each bookmark using the GoTo dialog, you could track which tables have been cross-referenced. (This will not, though, tell you that the cross-reference is still in the text, has not been deleted. To do that, you would need to check the REF fields for the bookmark.)
Workaround:
Manually add bookmarks to each Table Caption (i.e. naming the bookmarks Table01, Table02, etc.) and cross-reference to your bookmarks rather than to Tables. The bookmark is on the Caption, not the table. That way, you can see in the REF field that you have cross-referenced to a particular table, rather than a random number.
Find looking for «REF Table» with manually inserted bookmarks
Here is the Insert Bookmark dialog with manually inserted bookmarks and hidden unchecked.
Cross-Reference dialog with manually inserted bookmark
This is after hidden was unchecked in the bookmarks dialog. This does give you a non-printable list of all cross-references to the manually added Table bookmarks. If a cross-reference had been accidentally deleted, it would not show up in this list.
To add bookmarks to Table Captions
You can use Advanced Find to look for all captions that have the word Table in them.
Remember, you will want to select the entire caption (without the paragraph mark) to include in your bookmark. Insert bookmarks can be found under the Insert tab in the Links group.
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
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. Learn more about Allen…
MORE FROM ALLEN
Direction Arrows Confused
What do you do if you open a document, only to find that the arrow keys don’t work the way that they should? The first …
Discover More
Noting the Workbook Creation Date
You may want to add, to your worksheet, the date on which a particular workbook was created. Excel doesn’t provide a way …
Discover More
Absolutely Positioning a Graphic
Want a graphic to appear at a precise place on the page? It’s easy to gain control by following the steps in this tip.
Discover More
About cross-referencing in Word – what is a cross-reference?
In this article, you will find general information about cross-referencing in Word. You will learn how cross-reference fields in Word work, how to insert a cross-reference, etc. The information applies to both cross-references you insert using Word’s own functionality and using the Word add-in DocTools CrossReferenceManager.
The DocTools CrossReferenceManager add-in makes it easier and faster to work with cross-references in Word than using the built-in functionality. However, cross-references you have inserted in a document using DocTools CrossReferenceManager work precisely as if they had been inserted using Word’s built-in functionality.
This article is relevant for Word 2010, Word 2013, Word 2016, Word 2019, and Word for Microsoft 365.
What is a cross-reference in Word?
In general, a cross-reference is a note in a text that tells you to look somewhere else in the text for more information.
In Word, you can insert dynamic cross-references that can be updated if the text you refer to changes.
Technically, a cross-reference in Word is a field, i.e. set of codes that instructs Word to automatically insert material into a document. The material inserted by cross-reference fields can be text, section numbers, paragraph numbers, caption numbers, caption labels, etc. The field code can also include special information (referred to as switches) that make the field act or look in a special way. For example, a cross-reference field may function as a hyperlink so you can jump directly to the target of the cross-reference by clicking the field.
As opposed to cross-references you type manually, the great advantage of using cross-reference fields is that you only need to update fields to have the cross-references corrected if you have made changes to the document.
Cross-reference example. This example has three cross-reference fields referring to the paragraph number, paragraph text, and page number of the target for the cross reference. In order to insert a cross-reference like the one in this example using Word’s own cross-reference feature, you have to insert three separate cross-references in three operations and type the surrounding text. The DocTools CrossReferenceManager add-in lets to do it all in one operation. The gray shading is the result of having field shading in Word turned on. It is visible on the screen only and does not print.
How to insert a cross-reference in Word?
Note that you can only insert cross-references to content that already exits in the document. For example, you cannot insert a cross-reference to a heading that has not yet been added to the document. In case you want to refer to something in another document, you can create a hyperlink.
The purpose of the article is not to go into detail about how to insert cross-references in Word but to explain how cross-references work. For completeness, here are the steps to follow to insert a cross-reference using the built-in functionality of Word:
- In your document, position the insertion point where the cross-reference is to be inserted.
- In the Ribbon, select References tab > Captions group: Cross-references.
Note that you will also find the command in Insert tab > Links group: Cross-references.The following takes place in the Cross-reference dialog box. See the illustration below.
- In the Reference type list, select the type you want.
You can select from: Numbered item, Heading, Bookmark, Footnote, Endnote plus caption types depending on which caption labels are available (e.g. Figure, Table, Equation). Note that the dialog box shows all types no matter whether there are any targets of the different types in your document.
- In the Insert reference to list, select what type of content you want the cross-reference to show.
The items in the list depend on what you selected as the reference type in step 3.
- Turn on Insert as hyperlink if you want the cross-reference field to function as a hyperlink so users can click or Ctrl-click it to jump to the target.
- In the For which list, select the item the cross-reference must refer to.
The list is empty if no items match the reference type you selected. - Click Insert.
The built-in Cross-reference dialog box that lets you insert cross-references in Word. The targets in the For which list depend on the reference type you have selected and of the content in your document.
When you have clicked Insert in the Cross-reference dialog box, a cross-reference field is inserted in your document. Note that you must update cross-references yourself if you make changes to the document that influence the cross-reference targets.
The procedure above includes the main steps needed for most cross-references. In the Cross-reference dialog box, the Separate numbers with and Include above/below check boxes are enabled under some conditions. They are used for special purposes and are not covered here.
If you want to insert cross-reference constructions like «See Section 1.2, «This is the title», page 14″, you will need to type the surrounding text and repeat the steps above three times because you need a cross-reference to three items: The paragraph number (1.2 in the example), the paragraph text («This is the title» in the example) and the page number (14 in the example). On the other hand, the DocTools CrossReferenceManager add-in lets you insert entire cross-reference constructions like «See Section 1.2, «This is the title», page 14″ in a single operation.
What to do if headings are missing in the Cross-reference dialog box?
In the illustration above, you can see that Heading is selected as the reference type in the Cross-reference dialog box. You can see that several headings are listed in the For which heading field.
If your document has a number of headings and if all or some of the headings are missing in the dialog box, you need to check the formatting of your document. All paragraphs that are formatted with one of the built-in styles Heading 1-9 are automatically shown in the dialog box. If you have used other styles for the headings, they will only appear in the heading list if the outline level of each of the headings has been set to one of the levels Level 1-9 in the Paragraph dialog box > Indents and Spacing tab > Outline level field. Headings with the outline level is set to Body Text will not appear in the target list.
Note that the style names of Heading 1, Heading 2, … , Heading 9 are language-specific. If your Word isn’t English, you will see another word than «Heading» but still with the numbers 1-9. For example, the styles are named Overskrift 1, Overskrift 2, etc. in Danish.
See also Shauna Kelly’s article Why use Microsoft Word’s built-in heading styles?
Are there different types of cross-reference fields?
Let’s look a bit closer into the cross-reference fields. When you insert a cross-reference, the field code inside the field will depend on what the reference refers to. There are three types of cross-reference fields in Word. The first part of the field code of a cross-reference field tells which type of field it is. The field type can be one of the following:
- REF
- PAGEREF
- NOTEREF
You can read more about the syntax later in this article.
Below, you can read about how the three types of cross-references work.
A cross-reference field that directly or relatively refers to the page on which the target is found is of the type PAGEREF. When you select “Page number” as the “Reference to insert” in the built-in Cross-reference dialog box in Word or in the Insert Cross-reference dialog box in DocTools CrossReferenceManager, the inserted cross-reference field will be of the type PAGEREF. Also, the custom text placeholders {P} and {P_a/b} that can be used in DocTools CrossReferenceManager will insert PAGEREF fields.
A cross-reference field with a footnote or an endnote as the target will be of the type NOTEREF.
All other cross-reference fields will be of the type REF.
How does a cross-reference field know which content to show?
In order for a cross-reference field to show the correct content, it must include information that makes this possible – and that is precisely what it does. The field code inside a cross-reference field refers to a bookmark that points out the target
A bookmark in Word is a named location or a named block of text or other content in a document.
The field code inside a cross-reference field includes a reference to a bookmark that acts as the target of the cross-reference. See the illustration below.
If you use the built-in functionality in Word for inserting e.g. a cross-reference to a heading, Word automatically adds a bookmark around the heading text, excluding the paragraph mark, if such bookmark is not already found. The name of the bookmark is included in the field code of the cross-reference field. Such automatically added bookmarks are named _Ref followed by eight or nine digits.
When inserting a cross-reference to a caption, a bookmark will also be used to enclose the part of the caption content that is to be displayed by the cross-reference field. The bookmark will enclose different parts of the caption depending on the kind of caption reference you select (e.g. the entire caption or only the label and number).
Example of cross-reference bookmark name
_Ref123456789
The underscore in start of the bookmark name results in the bookmark being handled as a hidden bookmark in Word. You cannot add hidden bookmarks manually, i.e. the built-in Bookmark dialog box doesn’t let you start a bookmark name with “_”.
Hidden bookmarks remain invisible on the screen even if you turn on display of bookmarks. In the Bookmarks dialog box, you must turn on Hidden bookmarks to have the hidden bookmarks listed in the dialog box.
The Bookmark dialog box showing bookmarks for cross-references (names start with _Ref) and table of contents (names start with _Toc).
You can see a list of bookmarks in a Word document in the Bookmark dialog box. To open the dialog box, select Insert tab in the Ribbon > Bookmark or press Ctrl+Shift+F5.
Note that names of bookmarks added by DocTools CrossReferenceManager follow the same syntax rules as the bookmarks added by the built-in cross-reference functionality.
Example of cross-reference field of the type REF showing the field result and the field code. In this example, the h switch is included which means that the field works as a hyperlink so that you go to the bookmarked target when clicking or Ctrl-clicking the field (depending on your Word settings).
The Bookmark dialog box showing the related _Ref bookmark.
What is the syntax of the field code in cross-reference fields of the types REF, PAGEREF and NOTEREF?
REF field code syntax
The syntax for REF fields is:
{ [REF] Bookmark [* Format Switch ] [Switches ] }
The format switch and switches parts are optional.
Examples:
PAGEREF field code syntax
The syntax for PAGEREF fields is:
{ PAGEREF Bookmark [* Format Switch ] [Switches ]}
The format switch and switches parts are optional.
Examples:
NOTEREF field code syntax
The syntax for NOTEREF fields is:
{ NOTEREF Bookmark [* Format Switch ] [Switches ]}
The format switch and switches parts are optional.
Examples:
How to format cross-references?
You may want cross-references in a Word document to stand out from the surrounding text. You can use switches to change the formatting of cross-references. As mentioned above, a «switch» in a Word field is special information that makes the field act or look in a special way.
Note that the DocTools CrossReferenceManager can automatically format cross-references. For example, you can automatically apply a style to cross-references you insert. For help on manually formatting cross-references, see Formatting Cross-references.
How to find information about the field switches?
As mentioned above, a «switch» in a Word field is special information that makes the field act or look in a special way.
In the general help on Word, you can find information about the different types of switches that can be used with specific fields.
The fastest way to find this information is often to search using your preferred browser. You can search for «field codes [TYPE OF FIELD] field» (example: «field codes ref field») or something similar. You will find a full list covering all field types in the Microsoft article List of field codes in Word.
Are cross-references updated automatically?
Note that cross-references do not update automatically. If cross-references refer to headings, bookmarks, numbers or other targets that have been changed, you need to update the fields to reflect the changes.
You can update cross-references manually by selecting all (Ctrl+A) and pressing F9. Cross-references will also be updated when you switch to Print Preview or when you print if the Word option File > Options > Display > Printing options: Update fields before printing is turned on.
For detailed information about how all types of fields are updated, see my article Updating Fields in Word – How it Works.
How to fix broken cross-references that do not show the correct content?
You may experience that one or more cross-reference fields in a document do not show the expected content even if you have updated fields.
For example, you may experience problems of the following types:
- A cross-reference is missing part of the text it should have shown
- A cross-reference to a numbered heading shows 0 instead of the expected number or shows a wrong number
- A cross-reference includes more text than expected
- A cross-reference shows an error, telling that the reference source is not found.
If you run into such problems, my article Cross-reference Problems – Troubleshooting on my website thedoctools.com can help you understand the cause of the problems and help you solve them. The article includes videos that illustrate what causes the problems and how to solve them.
Note that my add-in DocTools CrossReferenceManager can help you automatically find and fix cross-reference problems. It can even prevent some of the problems from occurring.
Generate complete documents in seconds from re-usable text or graphics
Manage comments in Word fast and easy – review comments, extract comments to Word or Excel, etc.
Simplify and speed up the management of cross-references even in your most complex documents
Manage and repeat data in Word fast and easy with custom document properties and DocProperty fields
Extract insertions, deletions and comments from any Word document, incl. context and headings
Apply any highlight color or remove highlight in Word with a single click – customizable shortcuts
Browse pages, sections, headings, tables, graphics, etc. and find text in Word with a single click
Check safety-critical procedure documents for human factor issues in minutes – improve quality and help prevent errors
Create screen tips in Word fast and easy – with up to 2040 characters
Understanding cross-referencing in Word and how cross-reference fields in Word work can save you from a lot of time and trouble with Word documents. I hope this article helps you in your future work with cross-references in Word.
11-23-2012, 08:41 PM |
|||
|
|||
Cross References Using Find Replace Fellow Forum Members, The original creator of this book manually typed in the text, «Paragraph 5.4.3» all throughout the body of the manual and did not bother using the Insert -> Cross References. My objective is to convert all 40 instances of the text, «Paragraph 5.4.3» to Cross References Fields using the Find/Replace Tool. Is this possible? If yes, how do I tell WORD to find each instance of the text, «Paragraph 5.4.3» and then replace it with a Cross Reference Field containing the special code needed that points it to a tagged Heading 3 paragraph with the number prefix of 5.4.3? The alternative is to manually setup using each cross reference which is something I would like to avoid. Any help will be greatly appreciated. Thanks in advance. |
11-23-2012, 09:49 PM |
Hi Binar, Insert a cross-reference anywhere in the document. Cut it (eg via Ctr-X) to the Clipboard, then do a Find/Replace where:
__________________ |
11-24-2012, 06:29 PM |
|||
|
|||
Followup Quote:
Originally Posted by macropod Hi Binar, Insert a cross-reference anywhere in the document. Cut it (eg via Ctr-X) to the Clipboard, then do a Find/Replace where:
Macropod, In short, this trick is a tremendous time saver in my situation because I don’t only «Paragraph 5.4.3′ to change but a bunch of other paragraph number references that repeat all over the manual. I estimate I have at least 1,000 paragraph references to setup. Sure beats using the Cross Reference Dialog box and doing them one by one. Again, thanks a million. Cheers. |
11-26-2012, 09:48 AM |
|||
|
|||
What’s my error? I am trying to replicate this. However, in creating the original cross-reference to the heading, Word keeps giving me a simple 0 as a field. I haven’t experienced this before. Thanks. |
11-26-2012, 01:36 PM |
That may reflect the presence of some corruption in the document.
__________________ |
01-10-2014, 06:49 AM |
|||
|
|||
Quote:
Originally Posted by macropod Hi Binar, Insert a cross-reference anywhere in the document. Cut it (eg via Ctr-X) to the Clipboard, then do a Find/Replace where: Hi Paul(or anyone else answering), In regards to the original question, how do I change all *different* texts into different corresponding cross-references? E.g., I have a text originally written with plain text, like «see fig. 1», «see fig. 2,» «see fig. 3,» etc. Thanks in advance, |
01-10-2014, 02:59 PM |
Quote:
Originally Posted by tzachjade how do I change all *different* texts into different corresponding cross-references? You do them one at a time. Whilst a macro could be used, it would have to be pre-populated with each & every one of the cross-references that you want to use. Developing such a macro might well take longer than doing the process manually.
__________________ |