Easily Update All Fields in Microsoft Word (Table of Contents, etc) By Creating a Macro and a Button

Disclaimer: Kindly follow this tutorial carefully. I will not be held liable in any way for any misconfiguration mistakes that you, my dear reader, might make.

Note: In order for this to work, enabling macros in the options is a must. If you prefer not to enable macros, then this tutorial is not for you.

Follow the instructions below to add a Table of Contents update button to the Quick Access Toolbar in Microsoft Word (2013 and earlier), and make it work across all documents. By “all documents”, we mean documents that have been created, and documents that are going to be created on the same computer in the future. In reality, all fields such as the Table of Figures will be updated too, not just the Table of Contents, and that’s a good thing.

Let’s take it step by step

STEP 1

Create a new Word Document and open it.

STEP 2

Press ALT+F11 on the keyboard. This will open the Microsoft Visual Basic for Applications window. If you have trouble with the keyboard shortcut ALT+F11, activate the “Developer” tab by doing this:

– Click the File tab.
– Click Options.
– Click Customize Ribbon.
– Under Customize the Ribbon and under Main Tabs, select the Developer check box.
– In the Ribbon, click the Developer tab.
– Click Visual Basic

Again, this will open the Microsoft Visual Basic for Applications window.

STEP 3

In the left pane, navigate to “ThisDocument” under “Normal”, as follows:

Normal
Microsoft Word Objects
ThisDocument

STEP 4

Double click on “ThisDocument”. An empty (most likely) VB script window will open.

STEP 5

Copy and paste the following code courtesy of Gilles into the VB editor that was just opened.

'' Update all the fields, indexes, etc. in the specified document.
Sub UpdateAllFieldsIn(doc As Document)
'' Update tables. We do this first so that they contain all
'' necessary entries and so extend to their final number of
'' pages.

Dim toc As TableOfContents
For Each toc In doc.TablesOfContents
toc.Update
Next toc

Dim tof As TableOfFigures
For Each tof In doc.TablesOfFigures
tof.Update
Next tof

'' Update fields everywhere. This includes updates of page
'' numbers in tables (but would not add or remove entries).
'' This also takes care of all index updates.
Dim sr As range
For Each sr In doc.StoryRanges
sr.Fields.Update
While Not (sr.NextStoryRange Is Nothing)
Set sr = sr.NextStoryRange
'' FIXME: for footnotes, endnotes and comments, I get
'' a pop-up "Word cannot undo this action. Do you want
'' to continue?"
sr.Fields.Update
Wend
Next sr
End Sub

'' Update all the fields, indexes, etc. in the active document.
'' This is a parameterless sub-routine so that it can be used
'' interactively.
Sub UpdateAllFields()
UpdateAllFieldsIn ActiveDocument

End Sub

STEP 6

Save and close the Microsoft Visual Basic for Applications window.

STEP 7

Next, to add a Quick Access Toolbar button for convinience, navigate to the Quick Toolbar (next to the Undo & Redo buttons at the top left).

STEP 8

Click the little down arrow

STEP 9

From the drop-down menu, choose More Commands…

STEP 10

The “Word Options” dialogue box will appear. Under “Choose commands from:”, select “Macros”.

STEP 11

In the rectangular box under that, select the Macro with the name “Normal.ThisDocument.UpdateAllFields”.

STEP 12

Click the “Add” button in the middle of the dialogue box. It will be added to the rectangular box on the right side.

STEP 13

(Optional but recommended step). Click on Project.ThisDocument.UpdateAllFields

STEP 14

Click the button “Modify” at the button to change the symbol shape. Choose any symbol you fancy, and change the display name to whatever name you prefer. This name will be shown when you hover over the button in the Quick Access Toolbar. A name such as “Update All Fields” or “Update TOC” are some suggestions.

STEP 15

Click OK on the “Modify Button” dialogue box.

STEP 16

Click OK on the “Word Options” dialogue box.

STEP 17

Check that the button has been added at the top left in the Quick Access Toolbar.

STEP 18

Check the button’s functionality by creating a Table of Contents or a Table of Figures, then change a few things in the headers or figure captions, and click the button to see the updates reflected.

NOTE
This must be done once on every computer you use. If you have a desktop and a laptop, you must do all the above on both machines if you desire to have this functionality on all your computers. The reason is that this a matter of personalizing Microsoft Word, and such customizations are saved independently on each machine.

Thank you for reading!

References
http://superuser.com/questions/196703/how-do-i-update-all-fields-in-a-word-document
http://addbalance.com/word/normaltemplate.htm#Trouble_using_Alt+F11
http://www.msofficeforums.com/word/7034-does-word-have-option-update-fields-automatically.html#post27936
http://gregmaxey.mvps.org/word_tip_pages/add_macro_to_qat.html
http://www.gmayor.com/installing_macro.htm

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s