Creating a table of contents in your template.
In Microsoft Word, a table of contents is created in the following manner:
- Insert text in your document for the headings
- 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:

- Through the menu, choose "Insert>Reference>Index and Tables".
- 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.
|