6

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?

2 Answers 2

4

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.

2
  • harrymc your solution works perfect, in Word 2007, doesn't ask what to update just updates everything in the TOC. About update frequency, you can call the macro automatically upon document open and/or close : see this awesome site on Word macros (search 'AutoClose', 'AutoOpen').
    – Kharski
    Mar 22, 2013 at 12:47
  • TLDR: "press Ctrl+A and then F9" - sorted Dec 15, 2022 at 8:08
0

You will need a macro for this.

You have an example of such a macro here, used to update the "Save date" field. It will work with all fields in the document, and a Table Of Content is also a field so that should do the trick for you.

if you don't care about the "Save date" field (and thus the trick to save the doc twice so the "Save date" field is really updated), you can remove the following lines:

Dim bSaveDate As Boolean
bSaveDate = False

...

If oField.Type = wdFieldSaveDate Then
    bSaveDate = True
End If

...

If bSaveDate = True Then
    MsgBox "Save a second time to update a save date field"
End If

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .