Cross reference word vba

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's user avatar

0m3r

12.2k15 gold badges33 silver badges70 bronze badges

asked Jul 25, 2014 at 8:39

kafman's user avatar

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

Christina's user avatar

ChristinaChristina

1,3491 gold badge11 silver badges22 bronze badges

3

InsertCrossReferencesComfortably

VBA macro to comfortably insert cross references in MS Word

Screenshot
When inserting cross references in MS Word, have you ever been annoyed how tedious it is to do via the miniature default dialog (see screenshot above)? One has to

  • Navigate through the ribbon to find Insert -> Links -> Cross Reference (only visible if the Word window is very wide)
  • Click it
  • Select what shall be cross-referenced
  • Select how the cross-reference shall be displayed
  • Find the element in the list (which, prior to Office 2016 is diminutive and fixed in height !) and click it

How about that instead:

  • Press a hotkey when you want to insert a crossreference at the current location
  • Put the cursor to the element to which the crossreference shall point to, e.g. a figure label or a headline
  • Press the hotkey again => the cursor jumps back to the original location and the crossref is inserted

An additional feature is that you can configure yourself in which form the crossreference is inserted. This allows for combined inserts like the following (text in italics comes from field codes):

  • (see Figure 7 above, on page 8)
  • see above, chapter 1Introduction
  • cf. Table 2 (p. 123)
    You can configure multiple formats and toggle between them.

Preparation:

Quick way:

Download the Example file.docm (including macro), open it and play around with it.
However, item 1. from the ‘Proper way’ is mandatory.

Proper way:

  1. Make sure, the following References are ticked in the VBA editor:
    — Microsoft VBScript Regular Expressions 5.5
    How to do it: https://www.datanumen.com/blogs/add-object-library-reference-vba/
  2. Put the macro code in a VBA module in your document or document template.
    It is recommended to put it into normal.dot,
    then the functionality is available in any document.
  3. Assign a keyboard shortcut to this macro (recommendation: Ctrl+Alt+Q)
    This works like this (Office 2010):

    • File -> Options -> Adapt Ribbon -> Keyboard Shortcuts: Modify…
    • Select from Categories: Macros
    • Select form Macros: [name of the Macro]
    • Assign keyboard shortcut…
  4. Alternatively to 3) or in addition to the shortcut, you can assign this
    macro to the ribbon button Insert -> CrossReference.
    However, then you will not be able any more to access Word’s dialog
    for inserting cross references.

    • To assign this sub to the ribbon button Insert -> CrossReference,
      just rename this sub to InsertCrossReference (without underscore).
    • To de-assign, re-rename it to something like
      InsertCrossReference_ (with underscore).
  5. Adapt the configuration according to your preferences.

Useage:

  1. At the location in the document, where the crossreference shall be inserted,
    press the keyboard shortcut.
    A temporary bookmark is inserted (if their display is enabled, grey square brackets will appear).
  2. Move the cursor to the location to where the crossref shall point.
    Supported are:

    • Bookmarks
    • Subtitles of Figures, Abbildungen, Tables, Tabellen etc.
    • Headlines
      Recommendation for quickly jumping to headlines in large documents: use the navigation pane (View -> Navigation -> Headlines)
  3. Press the keyboard shortcut again.
    The cursor will jump back to the location of insertion and the crossref will be inserted.
    Done!
  4. Additional function:
    Positon the cursor at a cross reference field (if you have configured chained cross reference fields, put the cursor to the last field in the chain).
    Press the keyboard shortcut.

    • The field display toggles to the next configured option, e.g. from see Chapter 1 to cf. Introduction.
    • Subsequently added cross references will use the latest format (persistent until Word is exited).

Limitations:

  • Cross references to hidden text are not possible
  • The macro may fail trying to cross reference to locations that have heavily been edited (deletions / moves) with «track changes» (markup mode) turned on.

Revision History:

  • 151204 Beginn der Revision History
  • 160111 Kann jetzt auch umgehen mit Numerierungen mit Bindestrich à la «Figure 1-1»
  • 160112 Jetzt auch Querverweise möglich auf Dokumentenreferenzen à la «[66]» mit Feld » SEQ Ref «
  • 160615 Felder werden upgedatet falls nötig
  • 180710 Support für «Nummeriertes Element»
  • 181026 Generischerer Code für Figure¦Table¦Abbildung
  • 190628 New function: toggle to insert numeric or text references («r»)
  • 190629 Explanations and UI changed to English
  • 190709 Expanded configuration possibilities due to intermediate text sequences

Show All

InsertCrossReference Method

Inserts a cross-reference to a heading, bookmark, footnote, or endnote, or to an item for which a caption label is defined (for example, an equation, figure, or table).

expression.InsertCrossReference(ReferenceType, ReferenceKind, ReferenceItem, InsertAsHyperlink, IncludePosition, SeparateNumbers, SeparatorString)

expression    Required. An expression that returns one of the objects in the Applies To list.

ReferenceType   Required Variant. The type of item for which a cross-reference is to be inserted. Can be any WdReferenceType
or WdCaptionLabelID
constant or a user defined caption label.

WdReferenceType can be one of these WdReferenceType constants.
wdRefTypeBookmark
wdRefTypeEndnote
wdRefTypeFootnote
wdRefTypeHeading
wdRefTypeNumberedItem
WdCaptionLabelID can be one of these WdCaptionLabelID constants.
wdCaptionEquation
wdCaptionFigure
wdCaptionTable

ReferenceKind   Required WdReferenceKind. The information to be included in the cross-reference.

WdReferenceKind can be one of these WdReferenceKind constants.
wdContentText
wdEndnoteNumber
wdEndnoteNumberFormatted
wdEntireCaption
wdFootnoteNumber
wdFootnoteNumberFormatted
wdNumberFullContext
wdNumberNoContext
wdNumberRelativeContext
wdOnlyCaptionText
wdOnlyLabelAndNumber
wdPageNumber
wdPosition

ReferenceItem   Required Variant. If ReferenceType is wdRefTypeBookmark, this argument specifies a bookmark name. For all other ReferenceType values, this argument specifies the item number or name in the Reference type box in the Cross-reference dialog box. Use the GetCrossReferenceItems
method to return a list of item names that can be used with this argument.

InsertAsHyperlink   Optional Variant. True to insert the cross-reference as a hyperlink to the referenced item.

IncludePosition   Optional Variant. True to insert «above» or «below,» depending on the location of the reference item in relation to the cross-reference.

SeparateNumbers Optional Variant. True to use a separator to separate the numbers from the associated text. (Use only if the ReferenceType parameter is set to wdRefTypeNumberedItem and the ReferenceKind parameter is set to wdNumberFullContext.)

SeparatorString Optional Variant. Specifies the string to use as a separator if the SeparateNumbers parameter is set to True.

Remarks

If you specify wdPageNumber for the value of ReferenceKind, you may need to repaginate the document in order to see the correct cross-reference information.

Example

This example inserts at the beginning of the active document a cross-reference to the page that includes the first bookmark in the document.

Set myRange = ActiveDocument.Range(Start:=0, End:=0)
myBookmarks = ActiveDocument _
    .GetCrossReferenceItems(wdRefTypeBookmark)
With myRange
    .InsertBefore "Page "
    .Collapse Direction:=wdCollapseEnd
    .InsertCrossReference ReferenceType:=wdRefTypeBookmark, _
        ReferenceKind:=wdPageNumber, ReferenceItem:=myBookmarks(1)
End With
		

This example inserts a sentence that contains two cross-references: one cross-reference to heading text, and another one to the page where the heading text appears.

With Selection
    .Collapse Direction:=wdCollapseStart
    .InsertBefore "For more information, see "
    .Collapse Direction:=wdCollapseEnd
    .InsertCrossReference ReferenceType:=wdRefTypeHeading, _
        ReferenceKind:=wdContentText, ReferenceItem:=1
    .InsertAfter " on page "
    .Collapse Direction:=wdCollapseEnd
    .InsertCrossReference ReferenceType:=wdRefTypeHeading, _
        ReferenceKind:=wdPageNumber, ReferenceItem:=1
    .InsertAfter "."
End With
		

I’m looking for VBA code that would work for numbered item/paragraph number in cross-reference and insert something like 1.01(A) for example. This macro I found works great for inserting 1.01 number:

Sub InsertCrossRef()

Dim RefList As Variant
Dim LookUp As String
Dim Ref As String
Dim s As Integer, t As Integer
Dim i As Integer

On Error GoTo ErrExit
With Selection.Range
' discard leading blank spaces
Do While (Asc(.Text) = 32) And (.End > .Start)
.MoveStart wdCharacter
Loop
' discard trailing blank spaces, full stops and CRs
Do While ((Asc(Right(.Text, 1)) = 46) Or _
(Asc(Right(.Text, 1)) = 32) Or _
(Asc(Right(.Text, 1)) = 11) Or _
(Asc(Right(.Text, 1)) = 13)) And _
(.End > .Start)
.MoveEnd wdCharacter, -1
Loop

ErrExit:
If Len(.Text) = 0 Then
MsgBox "Please select a reference.", _
vbExclamation, "Invalid selection"
Exit Sub
End If

LookUp = .Text
End With
On Error GoTo 0

With ActiveDocument
' Use WdRefTypeHeading to retrieve Headings
RefList = .GetCrossReferenceItems(wdRefTypeNumberedItem)
For i = UBound(RefList) To 1 Step -1
Ref = Trim(RefList(i))
If InStr(1, Ref, LookUp, vbTextCompare) = 1 Then
s = InStr(2, Ref, " ")
t = InStr(2, Ref, Chr(9))
If (s = 0) Or (t = 0) Then
s = IIf(s > 0, s, t)
Else
s = IIf(s < t, s, t)
End If
If LookUp = Left(Ref, s - 1) Then Exit For
End If
Next i

If i Then
Selection.InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberFullContext, _
ReferenceItem:=CStr(i), _
InsertAsHyperlink:=True, _
IncludePosition:=False, _
SeparateNumbers:=False, _
SeparatorString:=" "
Else
MsgBox "A cross reference to """ & LookUp & """ couldn't be set" & vbCr & _
"because a paragraph with that number couldn't" & vbCr & _
"be found in the document.", _
vbInformation, "Invalid cross reference"
End If
End With
End Sub

but it doesn’t work for inserting level 2 or 3 from the cross reference list. Document is using multi level list so usually first level would be Section 1.01 for example , level two would be (A), (B), (C), level three (i), (ii), (iii), all followed by tab. Not sure if that’s possible, I understand that with number like 1.01 it’s easy because it appears only once in the cross reference list, but with (A) it’s more complex as it appears multiple times on the list and is not named as 1.01(A) for example. It just appears as (A) under each level 1. Is there a way to look for first instance of (A) for example under 1.01 on the list and then insert it as a cross reference? It would also have to be changed to paragraph number ( full context) so it inserts previous levels as well.

Hi guys.

I’m not a Word macro developer and I have a last task I cannot make on myown. From another application I’m genereting a large Word document. In that document there are strings like :

«ekran»
some chapter
Dependency

which are just textual references to a chapter in the document which has a title «some
chapter»

I need to make a Word macro to replace ««ekran»
some chapter
Dependency
» with cross-reference like «See ekran
some chapter on page xy.» What I can do is make the source application that generates the document to generate GUID of the chapter and make references like this:

«ekran»<GUID>
some chapter
Dependency

….

and the title of the chapter to be
(g)GUID(g)some chapter

so this way one could create a bookmark for each found
(g)GUID(g) which would be named with that GUID and then reference it in each string of type
«ekran»<GUID>
some chapter
Dependency

Using
the following code, used from another post, I know how to find this referencing text or how to find titles. I don’t know how to:


insert a bookmark at the given place


insert a cross-reference on that bookmark

here
is the code so far for finding textual refernces:

Selection.HomeKey unit:=wdStory

Dim rngPicture As Range
Dim strPicture As String
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
  Do While .Execute(FindText:="«ekran» ", Forward:=True, MatchWholeWord:=False, _
    MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
    Set rngPicture = Selection.Range
    rngPicture.End = ActiveDocument.Range.End
    rngPicture.End = rngPicture.Start + InStr(rngPicture, "Dependency")
    rngPicture.End = rngPicture.End + 9
    strPicture = Replace(rngPicture.Text, Chr(171) & "ekran" & Chr(187) & " ", "")
    strPicture = Replace(strPicture, "  Dependency", "")
    ' and now instead of the code below, here should be code for inserting cross references (text and page number)
    'ActiveDocument.Fields.Add rngPicture, wdFieldEmpty, "IncludePicture " & Chr(34) & strPicture & Chr(34)
  Loop
End With

Many thanks!

I am using Word 2010 and want to create a VBA script to insert a cross-reference.

I often insert a cross-reference of table numbers and figure numbers. So, I want to firstly show a dialog to answer which label, table, or figure to be inserted. Here, for example, I will type Figure. Then, I want to show another dialog to answer the table number or figure number. I will type 5.

Then, «Figure 5» should be inserted.

The VBA script I wrote is as follows:

Public Sub CrossReferrence()  
' To Insert Cross Reference  
'  
Dim reftype As String  
reftype = InputBox("The label to be inserted "Table" or "Figure" = ?")  
Dim refnum  
refnum = InputBox("Table number or Figure number = ?")  
If refnum <> "" Then  
    Selection.InsertCrossReference _  
        ReferenceType:="reftype", _  
        ReferenceKind:=wdOnlyLabelAndNumber, _  
        ReferenceItem:=refnum  
End If  
End Sub  

It seems that the Macro stops when it comes to the If sentence.
Please correct the Macro.

Like this post? Please share to your friends:
  • Cross out words which do not normally go together with the word in bold
  • Cross out the wrong word or words in each line and type the correct ones
  • Cross out the wrong word or phrase sometimes more than one option is possible
  • Cross out the wrong word in each group
  • Cross out the wrong word and type the correct ones the train