But since there's that nine-item limit, you're in for some serious scrolling if you need to make many references.In a Sisyphean spiral, the longer your document is, the more references you likely need, and the longer it will take to find each item in that teeny, tiny list.Place this macro in the template of your choice Sub Insert Auto XRef( ) Dim sel As Selection Dim doc As Document Dim v Headings As Variant Dim v As Variant Dim i As Integer Set sel = Selection Set doc = Selection. Count 1 Then Exit Sub ' Collapse selection if there are spaces or paragraph ' marks on either end sel. Move End While cset:=(Chr$(32) & Chr$(13)), Count:=-sel. This limitation is a problem if you have multiple headings with the same text, such as "The Code," used throughout this book. Name Exit Function End If Next i End If Set rng = para. Move End unit:=wd Character, Count:=-1 s Bookmark Name = Convert String Ref Bookmark Name(rng. Document ' Exit if selection includes multiple paragraphs If sel. Move Start While cset:=(Chr$(32) & Chr$(13)), Count:=sel. Second, the code offers no protection against creating a self-reference. Name, "XREF") Then Get Or Set XRef Bookmark = para. This hack shows you two ways to use VBA to automatically create cross-references to headings.In each case, the selected text is compared to the headings in a document, trying to find a match and create a cross-reference.
This code runs significantly faster than the code in the next section, but that speed comes at a price: you're limited to creating cross-references to items that Word considers potential targets, such as headings that use one of the built-in heading styles. Insert Cross Reference _ referencetype:=wd Ref Type Heading, _ referencekind:=wd Content Text, _ referenceitem:=i Exit Sub End If i = i + 1 Next v Msg Box "Couldn't match: " & sel. Text End Sub There are two important limitations to note about this code. This code is most helpful when assigned to a keyboard shortcut.
If you've also got a different kind of heading style in your document, such as Sidebar Title, those headings don't "count" as possible reference targets. Get Cross Reference Items(wd Ref Type Heading) i = 1 For Each v In v Headings If Trim (sel. First, if multiple headings match the selected text, the code creates a reference to the first match and ignores subsequent matches.
Using the Cross-reference dialog to insert references, particularly in a lengthy document, can be frustrating because it shows you only a few headings at a time.
This hack shows you how to create references automatically, without a visit to the dialog Cross-reference, depending on your version of Word) should display only nine items at a time clearly didn't have your best interests in mind.
method, which returns a list of potential reference targets in a document.
Because Word continually updates and indexes this list, accessing it is very fast. Add _ Name:=s Bookmark Name, _ Range:=rng Get Or Set XRef Bookmark = s Bookmark Name End Function This hack wouldn't be much of a time-saver if you had to go through a menu to run it.