I ran into a scenario where I needed to populate a word document which could contain a varying number of images from the output. I found this great video which showed me how to create content controls in the word document to act as a placeholder to accept dynamic content from power automate. The issue is I could not reliably get the repeating section to work to populate the varying images from my output. Below I will explain a different approach I took to get the result I wanted. You will need to create a template for each number of images you want to accept from your output. In my scenario I created ten templates. The first template contains one image acting as a placeholder and my tenth template contains ten images as placeholders. Anything over that I email the person a message saying only ten will be used.
Note: you will need the premium license for Power Automate to complete the below steps
I want to apologize ahead of time because my flow is much larger than what I am showing in this example so the screenshots may look out of order or seem out of place
In this example I will have two word documents
These templates I will have uploaded to a document list in SharePoint so I can access them from Power Automate
Create the type of flow you want (I chose instant flow)
Add an initialize variable action that will be used to create a temp folder
Value: (Choose any random number range you want, mine is below)
Next create another initialize variable action
Note: If you use string here the flow will error out because I think the file content gets added as an object. I don’t know how to deal with that so I just used Array and will use the expression first() to pull the first index of the TemplateFileContent. Open to any advise on this to simply use the string.
The last variable we will create is called Imagex
Now you will want to look inside your word document to see what number to start from. In my production flow I start with a value of 5 because all of the images prior to that are static. To check this for your template simply rename it to .zip and browse to word/media. In this example image1.png is the content control which I don’t currently use in production templates. image2.jpg is my placeholder image in Test Template 1.docx (renamed to Test Template 1.zip)
Note: When creating your template I would recommend you are consistent with your picture placeholder file types. So use all .png or .jpg file types.
Using what I know above now I will enter the value as 2 because that is my starting picture to replace
Next create a compose action to count how many images are in your output. For my use I have a previous action called Get Rendering files (properties only) and I am using the length() expression on that output to count how many images that action returned
The input for my compose action is this:
So you will need to do something similar to get the count on the number of images of your output
Next we create a switch action on the Outputs of the above compose action and create two cases
On each Case I add the Populate a Microsoft Word template action and select the template relevant to the number of pictures for that case
After that I create an append to array variable to store that template content into a variable
As you can see above my plain text content control (called test) I added shows up so I can add dynamic content in that field if I want. Also the picture content control is there and it does work but when I start to add several more I get spotty results. For giggles click here to see a picture of the template I have will several plain text content controls. Those work great.
Next add an action called Create file which will be used to put all the template information into a .zip
FolderPath: /Documents/TEMP (This is a SharePoint location to temp store the zip info)
Now we create an Extract folder action
Source File Path:
Destination Folder Path:
Next we will create an apply to each to get the file content of one picture, Resize Image(I am using a service to resize the image so that can be ignored), Create the file in the word/media folder over the placeholder image, then repeat
Select an output from previous steps:
(I only want the first 10 from my output)
The next step is zipping the information back up into a docx file. I can’t take any credit for this code and it looks like magic to me
How to create a Zip file in Power Automate for free (tachytelic.net)
Copy the below code then go to add an action and click My clipboard and press Ctrl+v and you should then see the CreateZip scope show up in your clipboard to add.
Copy above then go to My clipboard in the add action location and press Ctrl+v to paste.
After that expand settings and update the libraryPath and zipfolderPath
Library path is the path to the document library that contains the unzipped files form above
For example we will say my document list is located at https://contoso.sharepoint.com/sales/Shared Documents
You would enter “libraryPath”: “/sales/Shared Documents”
The zipFolderPath is the folder I want to zip and rename to docx
I think there was an issue with the Create SharePoint file action on this scope of code but I did not need it so I did not troubleshoot further maybe you won’t have an issue with it. I deleted the StoreZip and Create file action and added OneDrive Create file action
Notice the File Name field I added .docx at the end so the file would act as a word document
The final step is to cleanup the temp folder that was created
Add a Delete file action to delete the .zip file that was created earlier
Add a Get folder metadata using path then add Delete folder with the ItemId from the output of get folder metadata using path
After all of that you now should have a word document populated with various images!! There may be a more advanced way to utilize the xpath expression to manipulate the xml inside extracted word document but that is out of this scope and over my head a bit and this got the job done.
Xpath for reference if you want to try that route: