Table of contents

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

You don’t need to declare any loops. The templating engine is smart enough to understand the structure of the source object applied to your document. If you refer to an object property inside a collection, it understands that we need to iterate it.

Let us say you’re working on a company report, and you need to dynamically create document sections for every department and its employees. JSON representation of the object is:

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

Tokens in the template will look like this:

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

Just tag them as H1 and H2:

tag headings

Add a table of contents:

table of contents in DOCX template

The template on the left side will result in the document on the right side:

result toc in DOCX

To start the first section on the separate page, just add a page break under the table of contents in the template.

To avoid appearing sections on separate pages, you can place the tokens inside the table. You can use invisible borders. We kept them for the picture to show how it works:

use table

Conditionally hide sections

You can use hide-block-if formatter to hide entire sections based on a particular condition. A table of contents will get updated accordingly.

For instance, you’d like to hide sections on the development department and not include them in the table of contents.

To achieve that, add {{report.department}:hide-block-if(development)} to the main token and tag as H1:

hide sections conditionally

As a result, the entire section for the development department will be hidden and not reflected in the table of contents:

toc with the hidden section Update TOC in the result document

Automatically update table of contents on printing

If you specified PDF as an output type for this docx template then the table of contents will be updated automatically.

PDF output

Otherwise, you need to download the template and open it in the Word desktop version to make the changes.

Download template

Configure Word setting in the template to update files before printing. This will update the Table of contents on printing.

Update TOC Word settings

Automatically update table of contents on document open

You can add a macros to update the table of contents on open.

To use the method please follow the steps:

  • Open your DOCX template in the Microsoft Word desktop application and search for Visual Basic Editor.

Update TOC Macros
  • Copy and paste the code into the editor window.

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

Private Sub Document_Open()
Call TOCUpdate
End Sub
Update TOC Macros
  • To make the macros working, you need to save the template with .docm extension, otherwise the macros won’t be executed.

Update TOC Macros Update TOC Macros
  • After creating DOCX from the template in Power Automate, make sure to save the result with .docm extension.

Update TOC Macros

If you download the result DOCX file and open it, Word will ask you to enable editing, macros and update TOC automatically.


You may need to unblock the macros in the docx file settings after downloading. Please read more here.

Update TOC Macros