top of page

How to Modify or Remove a Repeating Section in a Word Document via Power Automate

This method is not documented and is a workaround to a suspected bug in the 'Populate a Word template' for Word Business action in Power Automate. For this method to work you must know how many items currently exist in the document's repeating section. Avoid doing this if there is an alternative method such as repopulating a template from scratch. If the underlying bug is resolved there could be unintended consequences using this approach.


Populating a Word template in Power Automate is a pretty straightforward process. However, if you need to iteratively populate the same document multiple times, then you must use a method that's less straightforward where you pass in a JSON object known as the dynamicFileSchema which contains key-value pairs referencing arbitrary IDs set in the Word document (described here).

This article will begin with the assumption that you understand that process and have found yourself stuck with a bug when attempting to insert or modify a repeating section in the template results in duplicated data rows in the populated template.


Let's fix that.


To begin, let me explain what re-inserting data into a populated repeating section actually does.

The below example invoice has a repeating section of four line items.

Let's assume I have populated this template from Power Automate via a canvas app and upon review I have entered some data incorrectly. I have drafted a write-up within the document already so populating a brand new document will make me lose that work. I need to only repopulate the line items to reflect changes made in my app.

If I feed in my updated JSON object of line items I notice weird behavior - instead of replacing the values in the repeating section it appears to cause duplicates (below)

However, let's take a closer look - it's not appending the new values, it is replacing the bottom item in the repeating section with the new contents. Here lies the bug we are trying to work around - inserting new data into a populated Word template has the unexpected behavior of replacing a record with all of the new data. Not replacing all of the old data with new data, not appending, but replacing one item with multiple new items.


After a lot of trial and error, I finally found something interesting - If I pass a null array into the repeating section it removes the topmost record of the file. If I do this multiple times (saving the file between each update) I can remove line after line until I'm left with only one last line which the final re-population of new data will overwrite.


So now that I have a method to remove one line, I need to ensure I'm only doing this n-1 times (where n = the number of lines in my existing Word document). If you remove all of your line items then the repeating section template control itself will be removed and you can't re-populate the section.

In other words, I must know that I have 4 line items so that I can loop 3 times to ensure I'm left with one line.


To remove the content from the final record if you want to reset the repeating section, pass a whitespace string ( " " ) into each column of the repeating section via the dynamicFileSchema.


Ultimately what I've created is this:

This flow takes in the previous line items to get a row count, skips the first loop (n-1) then proceeds to remove line by line until there is only one line left where it then repopulates the repeating section (overwriting the last line).


The main concept here is that repopulating a null array into a repeating section removes the top record, so keep this in mind if you are also trying to pass in a null record thinking that won't cause any updates. Repeating section modifications should be handled separately from other updates in a controlled flow to ensure no other modifications are happening to the section.

If you can avoid this by populating the original template and overwriting the output file you should do that, this should only be considered when you are bound to re-populating an existing populated document.

393 views0 comments

Recent Posts

See All

Commentaires


bottom of page