Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Office 365. 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.
by Allen Wyatt
(last updated August 10, 2019)
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:
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:
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, 2013, 2016, 2019, and Word in Office 365. You can find a version of this tip for the older menu interface of Word here: Updating an Entire TOC from a Macro.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
If you need to create a specialized table of contents, you need to know how to add TOC entries to your document. It's ...Discover More
Adding multiple tables of contents is a must for some types of document design. Here's a great overview of how you can ...Discover More
If you generate a table of contents for your document, there may be some unexpected surprises in the way the TOC appears. ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.