This post will explore how to merge ‘Approved’ Microsoft Word documents into a single file before converting the merged file into a PDF document. When merging the Word documents, we must also ensure that the table of contents has automatically updated to include any new pages.
This solution uses two Flowr actions:
The scenario for this post is that there is a SharePoint document library containing sales proposals, where each sales proposal is stored in a specific folder in the library. Team members working on the proposal will upload individual Word documents to the proposal folder. Once all the proposal sections are complete, a Power Automate flow is manually triggered to start an approval process. If all the documents are approved, they are merged into a single Word document and then converted to PDF before uploading to the proposal folder. If one or more documents are rejected, the flow will stop until the changes to the document(s) have been made and the flow has been re-run.
Jump to…
In the demo, we will be following Proposal 3. We can see that three different documents have been uploaded to the folder. The ‘Approval Status‘ column will be populated by the Power Automate flow depending on whether the document has been approved or rejected. The ‘Sort Order‘ column determines the order of the documents for the merge process.
The three documents look like this:
Section 1.docx
Section 2.docx
Section 3.docx
Only section 1 contains a table of contents because this will be the first section of the merged document, but note that the table of contents has only two entries.
A sales team member manually triggers The Power Automate flow once all the sections have been uploaded and are ready for approval. A text variable in the trigger called folderName keeps the flow dynamic so it can be used across all sales proposals.
A few variables are being initialised at the start of the flow:
We are also using a ‘Compose’ action to compose the folder path of the given folder using the folderName input variable. This path is used in some SharePoint actions further down the flow.
Next, we are using ‘Get files (properties only)‘ to get the properties of all the files in the folder. We can then adjust the order of the output array of that action using the sort expression in a ‘Compose’.
sort(outputs('Get_files_(properties_only)')?['body/value'],'SortOrder')
We sort the documents by the sort order number they are given. Sorting the documents at this stage means that they will be sent for approval in the correct order, making it easier for the approver to follow the document as they will be receiving each section individually.
Now, we can loop through the sorted array.
We first need to check that the document is not already approved, if it has already been approved it won’t need to be sent for approval again. Because we are looping through a composed output, we won’t get suggested dynamic content, so we need to write the expressions ourselves. The approval status can be selected by the following expression:
items('Apply_to_each')?['ApprovalStatus/Value']
Regardless of whether the document is already approved, we then need to get the document’s file content using the ‘Get file content‘ SharePoint action. The file identifier can be selected by the following expression:
items('Apply_to_each')?['{Identifier}']
If the document hasn’t already been approved, it needs to be sent for approval using the ‘Start and wait for an approval‘ action. Here, you can fill out any approval details you want to include and attach the document to be approved (it will appear as an attachment on the approval email). Make sure you use the filename with the extension property for the attachment name!
items('Apply_to_each')?['{FilenameWithExtension}']
We can add another condition after the approval step to check the approval outcome. If the document is rejected, the only action is to update the file properties to change the approval status to rejected. If the document is approved, we also need to update the file properties, but this time, we need to change the approval status to approved.
We then need to increment the approvedDocuments variable by 1, and append the current document details to the mergeDocumentInput array, ready to be merged. Below is the JSON format you need to append to the array:
{ "fileName": "file name with extension", "fileContent": file content, "wordInputFormatMode": "KeepSourceFormatting", "sortPosition": sort order }
If the document is rejected, there is no need to append to/increment these variables.
If the document has already been approved, we must increment approvedDocuments by 1, and append to the mergeDocumentsInput using the same format.
Now that all the documents have (if needed) gone through the approvals process, we need to check that all the documents in the folder have been approved before we merge them together. We can check if the approvedDocuments integer variable is the same as the length of the outputs of the ‘Get files (properties only)’ action we used at the start of the flow. If these values are equal, all the documents in the folder have been approved.
length(outputs('Get_files_(properties_only)')?['body/value'])
The last steps are to merge the documents, convert the merged document to PDF, and then save it back to SharePoint.
When using the ‘Merge Word Documents‘ Flowr action, you can input your data using the provided inputs or by providing the JSON array yourself. We already built our JSON array when we looped through the documents.
You can learn more about the ‘Merge Word Documents‘ action below:
Once we have merged the documents, we can then use Flowr’s ‘Convert to PDF‘ action using the outputted file content from the merge action as the input.
Lastly, we can use SharePoint’s ‘Create file‘ action to save the PDF back to the correct SharePoint folder. The ‘Folder Path‘ input is the file identifier we composed at the start of the flow.
As we can see from the image above, the final PDF contains all three Word documents merged. The table of contents has also been automatically updated to contain the new sections without any extra automation from Power Automate!
Check out Sophie’s companion video on YouTube!
https://youtu.be/elWsHBGH8Xo
Search 150+ Actions to see how Flowr can save you time
Sign up for your free 30-day trial; no cards, catches, or contracts.
No job is too big or too small for our Professional Services team!
UPDATE: We’re excited to announce some significant updates to Flowr for Power Automate! As of October 2024, we’ve improved by updating action names and splitting Flowr’s central Power Automate connector into nine specialized connectors. These changes will make your workflow faster, smoother, and more efficient. The new action names are more precise and intuitive, saving you time, while the focused connectors enhance performance and flexibility. This update also helps future-proof the platform for even more powerful features. Check out our updated action names blog.
Technical Evangelist