Table of contents

Modern templating engine

Still using Classic syntax? See the Classic documentation.

Learn the differences.

Plumsail Documents DOCX templates support table of contents. It’s possible to tag template tokens as headings to dynamically generate new sections of a document, or hide some of them conditionally. A table of contents will be updated accordingly.

To better understand how it works, let’s take a look at a couple of examples.

How it works

There’s no need to manually declare loops; the templating engine intelligently recognizes the structure of the source object in your document. When you reference a property within a collection, it understands that iteration is required.

In this example, we’ll create a company report that dynamically generates sections for each department and its employees.

We’ll use the following JSON data:

{
  "report": [
    {
      "department": "Marketing",
      "employee": "Derek Clark"
    },
    {
      "department": "Sales",
      "employee": "Jessica Adams"
    },
    {
      "department": "Development",
      "employee": "Anil Mittal"
    }
  ]
}

Let’s add the following tokens to our template:

{{report.department}} {{report.employee}}

Next, change them to H1 and H2 headings:

Modern Documents template with department and employee tokens tagged as H1 and H2 headings

Finally, add a table of contents:

Table of contents added to a DOCX template using the Modern templating engine

Here’s the result:

Generated DOCX document with department sections and an updated table of contents

To start the first section on a new page, add a page break beneath the table of contents in your template.

To keep sections from starting on new pages, you can wrap the tokens in a table and make the table borders invisible. We kept the borders visible for the picture to show how it works:

DOCX template with TOC section tokens wrapped in a table with invisible borders

Conditionally hiding sections

You can use the #hide-if logical operator to hide entire sections based on specific conditions. This will also update the table of contents accordingly.

For example, if you want to hide sections related to the ‘Development’ department, you can do so by adding the following code next to your {{report.department}} token and tagging it as H1:

{{#hide-if report.department == "Development"}}

Modern DOCX template using hide-if to conditionally hide the Development section

This way, the full section for the development department will be hidden and will not appear in the table of contents:

Generated DOCX document with the hidden Development section removed from the table of contents

Automatically update the table of contents for printing

If PDF is your chosen output format for the DOCX template, the table of contents will update automatically.

PDF output with the table of contents automatically updated from a Modern DOCX template

For other output types, you need to open the generated DOCX in Microsoft Word and choose Update table.

Update the table of contents in the generated DOCX document in Microsoft Word

Make sure to enable the Update fields before printing option under Display → Printing options for the table of contents to refresh automatically upon printing.

Microsoft Word Display and Printing options with Update fields before printing enabled for the table of contents

Automatically update the table of contents upon opening the document

You can add a macro to your template to refresh the table of contents automatically. Follow these steps:

  • Open your DOCX template in Microsoft Word and navigate to the Visual Basic Editor.

Microsoft Word Visual Basic Editor opened to add a macro for updating the table of contents
  • Copy and paste the following code into the ThisDocument module in the editor:

Sub TOCUpdate()
Dim TOC As TableOfContents
For Each TOC In ActiveDocument.TablesOfContents
TOC.Update
Next
End Sub

Private Sub Document_Open()
Call TOCUpdate
End Sub
VBA editor with the macro code for updating the table of contents
  • Save the template as a .docm file to enable macro functionality:

Microsoft Word warning that the VBA project cannot be saved in a macro-free document Microsoft Word Save As dialog with Word Macro-Enabled Document (*.docm) selected as the file type
  • After generating a DOCX from the template in Power Automate, make sure you save it with the .docm extension:

Power Automate action saving the generated document with the DOCM extension

When you open the result file, Word will prompt you to enable editing and macros. Click Enable content to update the table of contents.

Note

You may need to unblock the macros in the DOCX file settings after downloading. See this article for more information.

Microsoft Word security notification prompting the user to enable content and macros to update the table of contents