VBA Loop Through and Update all TOC (Table of Contents)

OzPanda

New Member
Joined
Mar 1, 2012
Messages
19
Hi just wondering does anyone have the VBA code that lets you loop through all Table of Contents in a Word Document. and update them.

At the moment I am using this:

VBA Code:
ActiveDocument.TablesOfContents(1).Update
ActiveDocument.TablesOfContents(2).Update
ActiveDocument.TablesOfContents(3).Update

But I was wondering if there's a way to determine who many tables there are in total and loop that many times so it would work for any document.

Sometimes there's TOC, table of figures, table of table so 3 but sometimes there's only 1.

Any help is greatly appreciated.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Untested, but maybe this:
VBA Code:
Dim i As Long

With ActiveDocument
    For i = 1 To .TablesOfContents.Count
        .TableOfContents(i).Update
    Next i
End With
 
Upvote 0
it errors on

.TableOfContents(i).Update
Run-time error '438':

Object doesn't support this property or method.

Ahh spelling issue: TablesOfContents

Cheers it works now.
 
Upvote 0
Thread closed. Many Thanks @Akuini.

Note: if anyone is reading this. Code works perfectly well if only one word doc opened if multiple word docs opened TOC may not update in the word doc you intended.
 
Upvote 0
OzPanda: As written, the code applies to the ActiveDocument, which means the document that has focus. You could probably get the same result with:
ActiveDocument.Fields.Update
regardless of how many Tables of Contents there are.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top