VBA macro to comfortably insert cross references in MS Word
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 1 - Introduction
- cf. Table 2 (p. 123)
You can configure multiple formats and toggle between them.
Download the Example file.docm (including macro), open it and play around with it.
However, item 1. from the 'Proper way' is mandatory.
- 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/ - 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. - 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 from Macros: [name of the Macro]
- Assign keyboard shortcut...
- File -> Options -> Adapt Ribbon -> Keyboard Shortcuts: Modify...
- 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).
- To assign this sub to the ribbon button Insert -> CrossReference,
just rename this sub to InsertCrossReference (without underscore).
- Adapt the configuration according to your preferences.
- 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). - 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)
- Press the keyboard shortcut again.
The cursor will jump back to the location of insertion and the crossref will be inserted.
Done! - 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).
- 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.
- The macro cannot run on MacOS (see #3).
- Cross references must be plain inside the document body, but cannot be within textboxes (see #4).