Skip to main content

Helpcenter

Table of Contents Automatic Update

In general, to find the page numbers in the table of contents the entire document has to be graphically rendered and the only way to do that is to open the document in Microsoft Word. Specifically, there are 4 possible ways to update the table of contents but none of them are without problems:

  1. Mark the update fields flag in the document that tells Word to update the table of contents when opening the document. The problem with this approach is that a popup message will open when opening the document prompting the user to confirm the update instead of updating the table of contents automatically when opening the document.

  2. Use Word Automation Services to open the document in Word, update the table of contents and save the document in the background. The problem with this approach is that opening a document, updating the table of contents and saving the document can take substantially longer than creating the document, from half a second to several seconds depending on the size of the document.

  3. Add a macro to the Normal.dotm (which is the base template for new documents and is opened when Word opens) that updates the table of contents when opening a document. The problems with this approach are that: 1. The macro has to be added to the Normal.dotm of every Word installation and 2. The macro will update the table of contents of every document opened in Word.

  4. Add a macro to the document that updates the table of contents when opening the document. Just like marking the update fields flag, a security popup message will also open when opening the document prompting the user if it is safe to run the macro. Because this approach basically behaves the same as marking the update fields flag, DocxFactory currently has no support for this approach.

Table of Contents Automatic Update Exercise
  1. Open toc.docx in the DocxFactory/exercises/templates/ directory.

    Open the Bookmark dialog box to view the items in the template. There is a “toc” top level item for the table of contents page and a “title1” top level item with a “title2” sub level item with titles that will be in the table of contents (see picture below).

    image55.png
  2. The "Heading" styles mark the titles in the document that will be in the table of contents. The biggest "Heading 1" style marks the highest level title down to the smallest "Heading 5" style that marks the lowest level title. Although the table of contents does not show titles lower than 3 levels.

    Highlight the “Title1” paragraph and select the “Heading 1” style from the Styles group in the Home ribbon. Do the same from the “Title2” paragraph with the “Heading 2” style (see picture below).

    image56.png

Note: The “Heading” styles and all the styles in the document, font, size, color etc. can be modified by right-clicking the style in the Styles group and selecting Modify.

  1. Insert the table of contents.

    First place the cursor in the “toc” top level item at the beginning of the document. Then select the Automatic Table 2 from the Table of Contents drop down list in the References ribbon (see picture below).

    image57.png

Note: If the table of contents direction is right-to-left and you would like it to be left-to-right or vice versa then open the Page Setup dialog box in the Page Layout ribbon, select the Layout tab and set the direction in the Section Direction field.

  1. Compile the template.

  2. Create the .DOCX file.

Copy and run the code below.

USING DocxFactory.*.

DEFINE VARIABLE i AS INTEGER NO-UNDO.

DEFINE VARIABLE j AS INTEGER NO-UNDO.

WordProcessingMerger:setUpdateTocMethod(1).

ETIME(TRUE).

WordProcessingMerger:loadTemplate("/opt/DocxFactory/exercises/templates/toc.dfw").

WordProcessingMerger:paste("toc").

DO i = 1 to 2:

WordProcessingMerger:paste("title1").

DO i = 1 to 3:

WordProcessingMerger:paste("title2").

END. /* 1 to 3 */

END. /* 1 to 2 */

WordProcessingMerger:saveDoc("/tmp/toc.docx").

MESSAGE "Completed (in" ROUND(ETIME(FALSE) / 1000, 3) "seconds)".

CATCH oErr AS Progress.Lang.AppError:

MESSAGE oErr:ReturnValue VIEW-AS ALERT-BOX.

END CATCH.

The code introduces the setUpdateTocMethod in the WordProcessingMerger class (see details below).

WordProcessingMerger:setUpdateTocMethod Method

Sets the method to use to update the table of contents if there is a table of contents in the document.

Declaration:

METHOD STATIC PUBLIC VOID setUpdateTocMethod(

INPUT piMethod AS INTEGER ).

Values:

0 – Turns off the DocxFactory table of contents automatic update. This method can be used to update the table of contents with a macro in the Normal.dotm.

1 - Sets the update fields flag in the document that tells Word to update the table of contents when opening the document. This is the default value if no value is set.

2 – Use Word Automation Services to open the document, update the table of contents and save the document in the background.

Notes:

  • The update table of contents method is a global setting that is not reset every time a new document is created.

    1. Open the created .DOCX file (see picture below).

      As you can see, a popup message opens when opening the document prompting the user to confirm the update.

      image58.png
    2. Set the update method to 2, run the code and open the created .DOCX file.

      As you can see, the table of contents was automatically updated and there was no popup message when opening the document but creating the document (and also updating the table of contents) took substantially longer this time.

      image59.png
    3. Set the update method to 0 and add a macro to the Normal.dotm.

      Open the Visual Basic for Word Editor from the Code group in the Developer ribbon. Select the NewMacros section in the Normal document, copy the AutoOpen macro below and save (see picture below).

      image60.png

Note: Remember that 1. this macro has to be added on every computer that can open created documents and 2. this macro will update the table of contents of every document opened on this computer.