WD: TOC Field Not Updated Properly from Macro

Last reviewed: February 23, 1998
Article ID: Q122555
The information in this article applies to:
  • Microsoft Word for Windows, versions 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows NT, version 6.0
  • Microsoft Word for the Macintosh, versions 6.0, 6.0.1
  • Microsoft Word for Windows 95, versions 7.0, 7.0a
  • Microsoft Word 97 for Windows
  • Microsoft Word 98 Macintosh Edition

SYMPTOMS

If you use a macro to update a TOC (table of contents) field, Microsoft Word may not prompt you to choose either Update Page Numbers Only or to Update Entire Table. Word only updates the page numbers.

CAUSE

Word defaults to Update Page Numbers Only.

WORKAROUND

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/supportnet/refguide/default.asp 

You can use a macro to replace the existing table of contents, and in effect, duplicate the Update Entire Table option programmatically.

The following sample macro locates the TOC field and uses the InsertTableOfContents command to replace the existing table of contents and retains all formatting previously applied to the original table.

   Sub MAIN
      StartOfDocument           'Move insertion point to start of document.
      OldVFC = ViewFieldCodes() 'Get the current state of ViewFieldCodes.
      ViewFieldCodes 1          'Turn on Field Codes.
      EditFindClearFormatting   'Clear EditFind formatting setting.
      EditFind .Find = "^19 toc"         'Find the first TOC field.
      InsertTableOfContents .Replace = 1 'Replace current table.
      ViewFieldCodes OldVFC              'Restore ViewFieldCodes setting.
   End Sub

NOTE: If you have converted your WordBasic macros to Visual Basic for Applications, the following routines demonstrate how to update a Table of Contents using Visual Basic for Applications:

   Sub UpdateTOC()
      ' Update the entire first Table of Contents
      ' in the active document.
      ActiveDocument.TablesOfContents(1).Update
   End Sub

To update only the page numbers of a Table of Contents:

   Sub UpdateTOC()
      ' Update only the page numbers of the first
      ' Table of Contents in the active document.
      ActiveDocument.TablesOfContents(1).UpdatePageNumbers
   End Sub

For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for Applications

REFERENCES

"Microsoft Word User's Guide," version 6.0. Document number WB51157-1093, pages 457-458 and 738.


Additional query words: macro toc table page numbers entire
Keywords : kbfield kbmacroexample kbwordvba macword ntword winword word6 word7 word8 word95 word97 wordnt macword98 macword6 word98 kbcode kbprg
Version : WINDOWS:6.0,6.0a,6.0c,7.0,7.0a,97; MACINTOSH:6.0,6.0.1,6.0.1a,98
Platform : MACINTOSH WINDOWS
Issue type : kbprb
Solution Type : kbwontfix kbworkaround


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 23, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.