0
votes

i have this code (button in ribbon) to update the TOC in my document (Word 2010 - Windows 7)

ActiveDocument.TablesOfContents(1).Update

, however it does not update the TOC

The only thing that does is the inbuilt "Update Table" command from the Reference menu.

Pressing the F9 key whilst the cursor is in the TOC does not work, nor does this code

Dim TOC As TableOfContents
With ActiveDocument
    For Each TOC In .TablesOfContents
        TOC.Update
    Next
End With

Any ideas what might be going on as it is very frustrating

1
Just to be sure, is the document containing the TOC definitely the ActiveDocument? Successful manual update implies the field is unlocked. (FWIW the one-line code works here, and fails with an error if there are no TOCs or I put the TOC in something such as a text box. Which is why I wondered whether your code could be running against another document). As an experiment, worth trying Activedocument.Fields(n).Update, where n is the number of the TOC field within the doc.user1379931
Using Activedocument.Fields(n).Update also fails to updatedjcamo
I would consider (a) experimenting with a completely new document containing a a single ToC, (b) repairing Office and (c) the possibility that the document is corrupt (in which case I would attempt repair.user1379931

1 Answers

0
votes

I wrote this a long time ago because of exactly this problem...

Option Explicit

' Update all the tables of contents, without annoying prompts.
Sub UpdateAllMyFields()
    Dim t As TableOfContents
    For Each t In ActiveDocument.TablesOfContents
        t.Update
    Next t
    ActiveDocument.Fields.Update        ' Update all fields.
End Sub

I use it whenever I have to deal with TOCs and fields. I also have update fields on Print option set.