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.
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:
Add a table of contents:
The template on the left side will result in the document on the right side:
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:
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:
As a result, the entire section for the development department will be hidden and not reflected in the table of contents:
If you specified PDF as an output type for this docx template then the table of contents will be updated automatically.
Otherwise, you need to download the template and open it in the Word desktop version to make the changes.
Configure Word setting in the template to update files before printing. This will update the Table of contents on printing.
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.
Copy and paste the code into the editor window.
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
To make the macros working, you need to save the template with .docm
extension, otherwise the macros won’t be executed.
After creating DOCX from the template in Power Automate, make sure to save the result with .docm
extension.
If you download the result DOCX file and open it, Word will ask you to enable editing, macros and update TOC automatically.
Note
You may need to unblock the macros in the docx file settings after downloading. Please read more here.