Tips and Tricks for populating Word templates using Power Automate

Phil Drake
Hitachi Solutions Braintrust
5 min readJun 16, 2020

Do you need to update any legal, contract or other Word documents dynamically? What about not only updating those documents automatically but also displaying certain sections based on conditions? This is possible with Power Automate. While there are several third-party connectors available at an additional cost, this article will walk you through some tips and tricks on how to update Word templates dynamically using the Word connector’s “Populate a Microsoft Word template” action. This is useful if your budget does not allow for additional cost for a third-party connector.

First, make sure your Word template includes Plain Text Content Controls in the places where you need Flow to either update with field values from Dynamics or update with the text you need. To add a Plain Text Content Control in Word, you must first display the “Developer” tab. To do this, go to File > Options > Customize Ribbon. Then under Customize the Ribbon and under Main Tabs, select the Developer check box and click OK.

Then to add the control, go to the “Developer” tab and choose the “Plain Text Content Control” option in the ribbon. Give your control a name because Flow will use this to enter data.

If you simply need to populate fields within that Word document, then you can enter Dynamics fields into the plain text content control fields displayed in the “Populate a Microsoft Word template” action after you’ve set it to pull from your Word template.

If you also need to show/hide sections of the document, then here are some tips and tricks to do so without having to go with a third-party solution. IMPORTANT: Please be aware that while this solution is great for a short document, it can become cumbersome and time-consuming if you have a really long and complex document with strict formatting requirements. If that is your requirement then you will be better off going with a third-party solution or possibly build an SSRS report:

1. Use the “Initialize a Variable” action

Set your variable Type to String and then, using the “Expression” tab in the Dynamic Content window, you can enter an IF expression to enter text into that plain text content control based on a condition. Here is an example of an IF statement that references a Dynamics field:

if(equals(triggerBody()?[‘prefix_fieldname’],’Yes’),’Here are the terms and conditions to claim rebate for the products listed below’,’Here are the terms and conditions to purchase the products listed below’)

Using this statement allows us to tell the Flow to update the document with different lines of text depending on what value a field is set to within Dynamics.

For example:

2. Use the “Compose” action

You can use Compose to “hold” any lines of text that you want to add to the Populate a Microsoft Word template later. For example, let’s say you’ve added a Condition Control early in your flow that will populate a section or field in your Word document but you will have additional conditions and/or variables later in your flow that you also need to use for the Populate a Microsoft Word template. Because you can’t use the Populate a Microsoft Word template action for the same Word document more than once in a Flow, that’s where this “Compose” action comes in.

For example:

3. Use the “Append to an Array” action

You would use Append to an Array when you have related records from Dynamics that you are referencing to populate a table in the Word template. Please see this article from Microsoft on how to add a repeating content control to your table in Word PRIOR to creating this action. In Flow, when related records are pulled from the “List records” action, it automatically creates “Apply to each” loops that allow it to know which related record it needs to check in Dynamics. The problem is, the Populate a Microsoft Word template action doesn’t let you reference actions you might have used within those “Apply to each” loops. This is where the Append to an Array is useful because Flow does allow you to reference an “Append to an Array” action in the “Populate a Microsoft Word template” action.

To use it, you must add an “Initialize a Variable” action earlier in your Flow, set the “Type” to “Array”, and leave the “Value” blank (the Append to Array action will populate that value automatically). Once the Initialize a Variable has been added, add the “Append to an Array” action where you need it later in the Flow and set the “Name” the name of the “Initialize a Variable” action you added earlier. Then under “Value” you can add code that looks like the following where “plaintextcontentcontrolname” is the name of whatever Plain Text Content Control you want updated in the Word template and “fieldname” is whatever Dynamics field you want it to pull a value from. Make sure you put quotes around the plain text content control name and DON’T put quotes around the field name. Also make sure you put a comma at the end of the fieldname if you have more than one line of fields to enter:

{

“plaintextcontentcontrolname”: fieldname,

“plaintextcontentcontrolname”: fieldname

}

Finally, when you go to enter your variable in the field in the “Populate a Microsoft Word template” action, you will need to click on the “Switch to array” text icon to the far right of the first repeating content control field. This will take all of the fields from that table in Word and combine them into one within the “Populate a Microsoft Word template” action.

For example:

Initialize a Variable

Append to an Array

Populate a Microsoft Word template

There you have it! This is a good alternative to populating Word templates dynamically without having to go with an expensive third-party solution or hire developers to code something.

--

--