Skip to content

VBA macro to comfortably insert cross references in MS Word

Notifications You must be signed in to change notification settings

boar24/InsertCrossReferencesComfortably

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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 1 - Introduction
  • 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 from 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.
  • The macro cannot run on MacOS (see #3).
  • Cross references must be plain inside the document body, but cannot be within textboxes (see #4).

About

VBA macro to comfortably insert cross references in MS Word

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages