Home     Products      Support      Corporate     Sign In 

Support Knowledge Base, Article 860

Product
WordWriter
Title
Updating the Table of Contents in a WordWriter-generated document
Problem

In Microsoft Word, a table of contents is not automatically updated when a document is opened. If a table of contents (TOC) is created and the document is modified afterwards (either in Word on the client, or using WordWriter on the server) the TOC will need to be updated in order to accurately reflect the changes. Otherwise, you may experience some of the following problems:

  • If your TOC points to MergeFields in your template, even though the mergefield will be correctly populated by WordWriter on the server, the TOC entry will display a copy of the original MergeField.
  • Page numbers may be inaccurate.
  • If your headers are generated using SetRepeatBlock, the TOC will show only those which which existed in the template at design-time.
The TOC can be updated manually by the end-user (by right-clicking on the TOC and choosing "update field"). However, a more user-friendly approach is to include VBA in your template which will cause the TOC to be updated automatically when the new document is opened on the client.

This article explains how to build a table of contents, and how to create a VBA macro which will refresh the TOC client-side.

Solution


Creating a table of contents in your template.

In Microsoft Word, a table of contents is created in the following manner:

  1. Insert text in your document for the headings
  2. Apply one of the heading styles to the text using "Format>Styles and Formats" from the menu, or the Styles dropdown in the formatting toolbar:



  3. Through the menu, choose "Insert>Reference>Index and Tables".
  4. Click on the "Table of Contents" tab to choose the format of your table of contents (for example, how many levels of headers you want to include, if you have nested headers). Word will automatically pick up all text with heading styles and put it into the TOC.

Below is an example of a template with a TOC which references a heading in a repeat block. The <<Heading>> in the table of contents is not a merge field - it is just picking up the text from the actual merge field in the document. When the WordWriter-generated file is opened on the client, the TOC will still look this way if it is not updated manually or through a macro.


Using Macros to update the TOC on the client

If you add the following macro to the Document_Open event of your template Word document, the table of contents will automatically update itself when the user opens the newly-generated file. If you have multiple TOCs, it will update all of them:

Private Sub Document_Open()

   For i = 1 To ThisDocument.TablesOfContents.Count
      ThisDocument.TablesOfContents.Item(i).Update
   Next
 
End Sub

After the macro runs, the TOC will reflect all the changes in the populated document:



Note: If you do not wish your users to get a macro security prompt every time they open the document, one option to consider is digitally signing your macros. See: http://office.microsoft.com/en-us/word/HA012317811033.aspx

The attached zip file contains the above template including the VBA/Macro code.

Related Links
OfficeWriter Home Page
OfficeWriter Enterprise Edition
Latest OfficeWriter News
OfficeWriter: Programmatic Runtime Control

Attachments
Attachments/KB860_files1.zip
Created : 5/28/2004 10:57:40 AM (last modified : 9/5/2007 4:11:37 PM)

Rate this article!

 
Comments



Copyright 2006 © SoftArtisans, Inc. All Rights Reserved.

Site Map     |     Privacy Policy     |     Contact Us