Microsoft Word 2007 – How to Auto-Update Table of Contents

microsoft-office-2007microsoft-word-2007

I am using Word 2007, but saving my documents in .doc (as opposed to .docx) formats because that's company policy.

I have the ToC set up fine, but is there a way to have it update automatically (at document open, save or otherwise)? Word help suggests that it should update upon opening the document, but that doesn't seem to happen.

Any ideas?

Best Answer

If there's a check mark on the option Tools > Options > Print > Update fields (in Word 2007, Office button > Word Options > Display > Update fields before printing), then going to Print Preview and back will update the fields. But it's just as easy to press Ctrl+A and then F9.

If you're looking for something that doesn't need any user interaction at all, then you need a macro.
An example of a macro to update all fields of type ToC is:

Sub TOCFieldUpdate()
' Written by Charles Kyle Kenyon 27 January 2005
' Field Updater - TOC fields
Dim oField As Field
On Error Resume Next
For Each oField In ActiveDocument.Fields
If oField.Type = wdFieldTOC Then
oField.Update
End If
If oField.Type = wdFieldTOA Then
oField.Update
End If
Next oField
End Sub

If you wish to periodically execute the above macro, here's another macro for that.
that will update the ToC every 5 minutes:

Public Sub ToCUpdate()
Call TOCFieldUpdate
DoEvents
Application.OnTime When:=Now + TimeValue("00:05:00"), name:="ToCUpdate"
End Sub

You can assign this macro to an icon or a hotkey. I wouldn't suggest to make it run automatically when the document opens, as you would run against the latest security safeguards of Microsoft.

Note: The above is untested, and even worse, is my very first attempt in writing VBA.

Related Question