Using Power Automate to Create Word Documents

Placing dynamic content into a Word Document isn't a new thing. Traditionally we would create an Excel spreadsheet containing some info, and then use Mail Merge to bring it across into our document. Moving on from there we then had the ability to create a Word document, and if we stored it in SharePoint then we could take some of the metadata and place that into the document. But now, the techniques to achieve dynamic creation of documents has moved on once again, and this is one of my favourite features of Power Automate.

Power Automate has a Word Online (Business) connector, which allows me to interact directly with content placeholders in my document. I'll point out now that this is a premium connector, so you'll need to license the user, process or app in order to use this, but when you understand the functionality you may find that it's worth it.

Preparing the Document

The first thing that you need to do with your Word document is prepare it to receive data from Power Automate. First of all open Word, and either start a new document or open a document where you want to inject content.

Along the top menu bar, you should see the Developer tab. If you can't see this tab, please read "Show the developers tab" on the Microsoft site. On the tool bar, you will see a group called "Controls" which allows you to select what type of content you want to place into your document.



For this example, I am going to select a Text Content Control, and a Picture Content Control. I am going to use both of these controls to create a postage label, so my text will contain a name and address, and my image will contain a QR code.

Once I have those controls in my document, I need to give them a title so that they appear with a "friendly" name when I use my Word actions in Power Automate. To give it a title, click on the relevant control, and then click Properties in the Controls group on the Developer menu.
Once you have given the control a title, press OK. Now repeat the same process for any other controls that you have placed on the page.

Save your document, I have called mine "Postage Label Template.docx" and upload it into SharePoint so that we can use it within our Flows.

Now that we have the template created, and uploaded to SharePoint, let's create a basic list to contain the detail we need to pass into our Word Document. I have created a basic list and added a column called Address.
Now that we have all of the constituent parts in place, let's go and create a Flow to populate create a Word document with the postage information populated.

Creating the Flow

The Flow which I will create will do the following:

  1. Trigger the Flow when an item is added to the Labels list
  2. Generate a QR code and save the image to SharePoint
  3. Populate the Word template with content

Let's start with creating the Flow and creating a new document based on the template.

Trigger

The Flow will be initiated by using the "When an item is created" trigger from the SharePoint connector. This will run each time a new item is added to the list. I will simply configure it to look at the "Labels" list.
Now that we have the trigger in place, let's generate our QR code.

Generate a QR Code

The first thing we are going to do is encode the address into a QR code in order to put it into our Word document later. So let's put the "Generate Basic QR Code" action, from the Vantage 365 Imaging Connector into the Flow, and tell it to encode the Address dynamic content from the trigger.


For the Microsoft Word document to accept the image returned, it needs to be transformed into a binary encoded PNG file. The easiest way to do this, is to create a file within SharePoint which we can then use later. In order to create the file, I need to isolate the Base64 String from the QR code. In this demo we'll do this in an "Initialize variable" step. In order to isolate the part of the string, I need to use an expression to split the string on the comma "," and take the second part only. 

split(body('Generate_Basic_QR_Code')?['qrcode'],',')[1]
Once I have the Base 64 string isolated, I can then use the "Create file" action from the SharePoint connector to create the file within SharePoint. Again I need to use a small expression to ensure that the string is in the correct format by using the base64ToBinary formula.
base64ToBinary(variables('Base 64 String'))
The unfortunate thing with this action is that I can't then get the encoded file content directly from the Create file action, so I need to put an action in to "Get file content". We can, however, use the ID from the Create file action to get the content of the file.
Now that we have the QR code, and we have it formatted in a way that Microsoft Word will accept, let's place populate the template.

Populate the Template

Now that we have all of the data we need, we can use the "Populate a Microsoft Word template" action from the Word Online (Business) connector. When we place it onto the screen, we need to select the template which we're going to populate. Once that has been done, it will understand what fields are available to be populated and you can then take the dynamic content from the previous actions and use them here. For the Address, we can use the Address dynamic content from the trigger, and for the QR code we can take the Content dynamic code from the "Get file content" action.
The final thing that we need to do, is to create a file based on the populated Word template. Again, we can simply use the "Create file" action to save the populate file, and store it within SharePoint.
Now that we have completed our Flow, you can trigger it and observe the output.

What we've created

Once our Flow has executed, it will take the Address from the SharePoint list and encode it in a QR code. That QR code is saved as an image and then pushed into a Word document which we've saved back to SharePoint demonstrating that we can create content within Power Automate, and use that to generate our document.

Finally...

Power Automate has a great connector available to it called Word Online (Business), which allows us to take a Word document and push some content into it. The content could be text, or it could be graphical, but using either of those is far superior to the ways in which we'd have achieved this in the past.

The Word for Business connector is a premium connector, but if you're generating documents automatically then this will pay for itself quite quickly, especially if it's part of a larger business process.

The Word document itself must first of all be prepared to receive the content, therefore you need to add the developer tab so that you can then add various controls to your content.

Using the "Populate a Microsoft Word template" action will create file content which can then be used with the normal Create file actions to save the document to SharePoint, OneDrive or any of the other cloud storage providers that you may want to use.

As always, I hope you found this blog useful, and I welcome any feedback.

Comments

  1. Thank you so much for sharing this worth able content with us. The concept taken here will be useful for my future programs and i will surely implement them in my study. Keep blogging article like this.
    Help Desk app

    ReplyDelete
  2. Anyone any idea why I cannot seem to connect to Word? I don't see it as one of the possibilities

    ReplyDelete
  3. When I did this I could not figure out where the filled template file went. How do we set it's save space?

    ReplyDelete
    Replies
    1. Hi Josh, when you have filled the template, it is the step where you Create File. Filling the template will give you some File Content, which you can then use as dynamic content using the SharePoint or OneDrive Create File action

      Delete
  4. You've provided quite good information here about Microsoft Office Home & Business 2019. This is fantastic since it expands our knowledge and is also beneficial to us. Thank you for sharing this piece of writing.

    ReplyDelete
  5. I'd like to express my gratitude for writing such an informative piece on this blog about remote pc spy software online. This article provided me with a variety of data. Continue to post.

    ReplyDelete

Post a Comment