Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, and 2013. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Updating an Entire TOC from a Macro.

Updating an Entire TOC from a Macro

by Allen Wyatt
(last updated May 30, 2016)

3

If you have a document that contains a table of contents (TOC), and you update the fields in the entire document, Word asks if you want to update the entire table, the chapter entries, or just the page numbers associated with the existing chapter entries in the Table of Contents. This occurs because TOCs are implemented through the use of a field, and when you update all fields you are telling Word you also want to update the field underlying the TOC.

You can update a TOC using a macro by utilizing the TablesOfContents collection. Each item in the collection represents a single TOC in the document. (In most documents the collection will consist of only a single item.) To update the entire TOC, you use this format of the command:

ActiveDocument.TablesOfContents(1).Update

The Update method is what does the work; it updates the TOC. If you want to update only the page numbers in the TOC, you use an entirely different method:

ActiveDocument.TablesOfContents(1).UpdatePageNumbers

Whenever you use commands like these in a macro, it is a good idea to make sure that there is actually a TOC in the document before you try to do any updating. The easiest way to do this is to just check the Count property for the collection, as shown here:

If ActiveDocument.TablesOfContents.Count = 1 Then _ 
  ActiveDocument.TablesOfContents(1).Update

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (8621) applies to Microsoft Word 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Word here: Updating an Entire TOC from a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Counting with PivotTables

One of the ways you can use PivotTables is to generate counts of various items in a data table. This is a great technique if ...

Discover More

Problems Pasting Large Pictures

If you insert a large picture in your document and your text jumps all around and the picture seems to disappear, don't ...

Discover More

Shortcut to Display Bookmarks

It can be helpful to see where the bookmarks are located in a document. This tip provides a quick way that you can both show ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Creating a Table of Contents Involving Multiple Documents

Word can handle very large documents, but some people find it preferable to break large documents into multiple documents. ...

Discover More

Using Dot Leaders in Special Tables

Adding dot leaders before page numbers in a table of contents or table of authorities can make the finished table look ...

Discover More

Creating a Table of Contents from Heading Levels

If your document is any length at all, adding a table of contents is a nice touch. This tip demonstrates how easy it is to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 7?

2016-07-16 08:20:16

bogdan

I continued my searches and I succeeded to find something useful (Paul McFedries: http://www.quepublishing.com/articles/article.aspx?p=731710&seqNum=2 ). The original macro saves a file automatically (actually what Word is also doing itself) and I pasted the line for updating TOC. Sorry again for my novice level regarding macros :)


2016-07-16 07:44:55

bogdan

I work with Word 2016 and no macros experience. I need a macro for automatically/continuously update TOC at certain time intervals (minutes). Including some guidance for inserting it in the Word file. I searched extensively a solution on internet but no luck. I truly appreciate any help on this matter. And I thank you very much in advance.


2013-06-17 09:46:15

Bryan

If you have an undetermined number of tables of contents at run time (i.e., like Allen's example where you need to check if one exists at all, or a situation where you may have more than one TOC), you can use this code instead:

Dim toc As TableOfContents

For Each toc In ActiveDocument.TablesOfContents
toc.Update
Next toc

Or, of course, if you just want the page nubmers, you can replace toc.Update with toc.UpdatePageNumbers.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.