cancel
Showing results for 
Search instead for 
Did you mean: 
Reply

Populating complex Word template

Hello,

 

I have been working on trying to populate a complex Word template with data from several SharePoint lists, but am running into difficulty with repeating rows inside of a larger item.  I'm not sure if this is a limitation within Word or Power Automate, but I think my issue is within Power Automate, so I am sharing all info.  In Power Automate, the Populate a Word Template does not work within a For each loop or any other control. As long as the final file is a PDF, I do not have to use Word, if there is another option. 

 

For a singular project, I can populate: Title (single line of text), Description (multiple lines of text), Product List (repeating rows of "Product" and "Status"), plus more of the same.

 

What I cannot do is repeat all the items listed above for multiple projects within the same Word doc: having Title, Description and Product List for Title1, Title2, etc. 

 

Examples

Can doCan't Do

Title 1

Description 1

Products

Product A, Completed

Product B, In process

Product C, Not started

Title 1

Description 1

Products

Product A, Completed

Product B, In process

Product C, Not started

 

Title 2

Description 2

Products

Product A, Completed

Product B, Completed

Product C, In process

 

I have tried creating an HTML document then replacing the fields just like in Word, where [title] is replaced by TitleName - and this works until I need to repeat for all projects.  I have also tried writing my own JSON (which has been validated), and it works again until the whole project needs to be repeated for all projects in the SharePoint list. 

 

I'm not sure what specific information you all need to help.  I have searched online for similar items and tried to modify Flows from Reza Dorrani , Laura Rogers and many more.  Laura has a video on looping through a task list - which is great, but I need more than just the repeating rows and I need it all in one file, emails and not multiple files.

 

Thanks,

Cheryl

1 ACCEPTED SOLUTION

Accepted Solutions

There isn't really a trick.  Its a bit tougher because you are working with RAW HTML.  But I've hand coded enough web sites over the years in notepad.  Its the same thing.  Here's screenshots of the flow.

 

1) Start the flow and get Base64 code for the two images I use in the HTML.

image.png

2) Create a bunch of other variables to hold different HTML fragments.  

image.png

The top of the document must have this HTML

image.png

The rest of the variables are HTML building blocks.  For example, here's the HTML fragment that starts the <Body> tag.  Dynamic content is applied where needed.  This header will only be used once in the document.

3) Now that we have the building blocks for the top and bottom of the document its time to buildout the detail lines. I create a loop counter so I can alternate the background colors of the rows to make them more readable.  Then process each row using an Apply to each loop.

image.png

Odd versus even rows have slightly different styles but are otherwise identical.  Here's what an odd row looks like.  This inserts the dynamic content for that row and appends it to the Table Header variable.

image.png

4) Once all the rows are processed I close out the HTML table that I've been appending rows to.  Then I concatenate the various fragments together, create the HTML file in OneDrive, and Convert it to PDF in memory, and save the PDF file to OneDrive.

image.png



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

View solution in original post

17 REPLIES 17

Hi @CCava 

 

I've done an article on creating a dynamic pdf document without using the premium word connector on my blog but it is a faff vs the permium connector so if you have that as an option, let's keep that option for now.

 

The sample data you have provided:

 

Title 1

Description 1

Products

Product A, Completed

Product B, In process

Product C, Not started

 

Title 2

Description 2

Products

Product A, Completed

Product B, Completed

Product C, In process

 

Is that all dynamic?  You could simply have 1 quick part field in your word document and do the data prep in Power Automate, would that work?  For instance if I stuck the above in a compose and then called it to the word action, it would complete the file exactly per above, no need to have multiple quick parts, unless there should be some formatting between, but even then, you could create all of this in PowerAutomate.

 

Alternatively you can convert HTML to PDF via the OneDrive Connector but it's not very realiable.

 

If the above works for your great, alternatively can you provide a bit more info, like a screenshot of the required Word Doc and the data source(s).

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Cheers,
Damien


P.S. take a look at my new blog here

Hi @DamoBird365,

 

Thanks for your reply.  Most of the content is dynamic, aside from "Products" in this case, which is just standard text.

 

When you say to stick it in a Compose, could you give more information.  I have used compose before for a few things such as replacing text - is that what you are thinking of? And sticking that in a for each loop for all titles in the list?

 

I have a lot of fields and lists in my data (all SharePoint lists), but it essentially boils down to what I'm showing below.  I do also have fields (title, quarter, and year) that I already filter the Get Items action using the filter query with an ODATA query - this portion is successful on my end, so I did not include it in my original request, but will here, in case it's needed/useful. Data is populated through a Power App.

 

Report Request List (when a new item is created here, it triggers the flow)

UserQuarterYearCreated Timestamp
User 112021[auto-generated]
User 212021[auto-generated]

 

Projects List (I need to filter this list by the User field to match the User who initiated the request in the list above to get only their projects)

UserProjectTitleDescriptionAnotherFieldAnotherField2AnotherField3
User 1Title 1Lorem ipsum 1   
User 2Title 2Lorem ipsum 2   
User 1Title 3Lorem Ipsum 3   

 

Products List (I need to filter this list 1st for the User who requested the report, then batch their products/status together)

TitleUserProductStatusAnotherField
Title 1User 1Product AIn process 

Title 2

User 2Product CCompleted 
Title 1User 1Product BCompleted 
Title 3User 1Product BNot started 
Title 1User 1Product CIn process 
Title 2User 2Product AIn process 
Title 4User 2Product AIn process 

 

The Word template would look like this

Notes

  • Anything in '{}' is a plain text user control (I use the curly braces to easily and quickly identify the fields)
  • The products list is a table with no borders
  • Product Type and Product Status have their own plain text control, plus are together in a repeating section control
  • I've tried to put everything for the project together in a control, in case that would help, but have been unsuccessful

CCava_0-1619609762432.png

 

 

I really appreciate your help on this - I've been working on this for a few weeks, on and off, but still a few weeks. Thanks again!

 

Cheryl

Hi again @DamoBird365,

 

I'm still having difficulties with this.  I've looked into the Quick Parts, but am still not sure on how to repeat the entire Quick Part within the same file. 

 

@Pstork1and @ScottShearer : I've either taken a SharePoint Virtual Workshop with you and/or seen you present at SharePoint fest (all wonderful!) - do either of you have suggestions on this?

 

Thanks!

 

Cheryl

Pstork1
Most Valuable Professional
Most Valuable Professional

I've done something similar for creating an Invoice with repeating sections in HTML and then converting to PDF.  The key is to divide the HTML up into fragments.  Then fill in each fragment.  Finally concatenate all the fragments together and convert them to a PDF.  For the Apply to each do the following:

 

1) have one HTML fragment that is the template

2) at the start of the loop copy the template to another variable

3) replace the values in the variable for that iteration

4) append the variable to the main document. Loop and repeat at step 2

5) after exiting the loop append a variable to the document to close out the HTML

 



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Thanks, @Pstork1! I will give this a try and if I am successful, I will mark it as the solution.  I appreciate your quick response!

Pstork1
Most Valuable Professional
Most Valuable Professional

Let me know if any of the steps aren't clear.  I can provide some screenshots of the example I mentioned if that will help.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Hi @Pstork1 

 

I'm interested 😂.  I've played with HTML to PDF in OneDrive and found the conversion hit and miss and had better luck with Word or Excel to PDF.  If there is a trick to getting the format correct I would appreciate you sharing the tip.

 

Cheers,

 

Damien

There isn't really a trick.  Its a bit tougher because you are working with RAW HTML.  But I've hand coded enough web sites over the years in notepad.  Its the same thing.  Here's screenshots of the flow.

 

1) Start the flow and get Base64 code for the two images I use in the HTML.

image.png

2) Create a bunch of other variables to hold different HTML fragments.  

image.png

The top of the document must have this HTML

image.png

The rest of the variables are HTML building blocks.  For example, here's the HTML fragment that starts the <Body> tag.  Dynamic content is applied where needed.  This header will only be used once in the document.

3) Now that we have the building blocks for the top and bottom of the document its time to buildout the detail lines. I create a loop counter so I can alternate the background colors of the rows to make them more readable.  Then process each row using an Apply to each loop.

image.png

Odd versus even rows have slightly different styles but are otherwise identical.  Here's what an odd row looks like.  This inserts the dynamic content for that row and appends it to the Table Header variable.

image.png

4) Once all the rows are processed I close out the HTML table that I've been appending rows to.  Then I concatenate the various fragments together, create the HTML file in OneDrive, and Convert it to PDF in memory, and save the PDF file to OneDrive.

image.png



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Hi, @Pstork1 ,

 

Sorry it has taken me so long to get back on this - other higher priority projects came up.  This has mostly helped (I've been able to successfully add most parts into the string variables, concatenate them, create the file and view it) , but I am still not successful with one part of my request though: filtering a list for just the project listed. 

 

In this report, I will have multiple projects that each have repeating rows sections (see example 1 below, data will match the screenshots for ease - please excuse my weird test data) - which I can get with what I created from your suggestions, but the data for the repeating rows section within each project is listing all data for all projects, not just the project it's part of (see image Results), so I tried to get the data a different way which somewhat works.

 

 

Example 1

Report for Cheryl C (will have a report for multiple researchers)

ABC Reading Rainbow, active

Some description of the project

Staff on the project

 

Book List

Book TitleBook Author
My Little PonyCool author
Strawberry ShortcakeAnother author

 

Outcomes List

ItemDue
Write paperJul 1
Write bookOct 31

 

123 Math Stories, active

Some description of the project

Staff on the project

 

Book List

Book TitleBook Author
The little engine that couldRad person
Best math stories everMath teacher
Bedtime mathParentsR Us

 

Outcomes List

ItemDue
Edit paperJul 15
Edit bookOct 31

 

Results

Results.png

 

My Flow

flow-All.png

 

Close up on appending book list variables which is the part not working how I want

appendBookLists.png

 

I tried filtering the data through Parse JSON then using Select to show only the fields I need then using Create HTML table - it still always lists the entire list, not just for that project.  Project title is the first column in the lists:

 

testData.png

 

I'd appreciate any help or insight you (or anyone else) can give into this  - Thanks!

 

Cheryl

 

Pstork1
Most Valuable Professional
Most Valuable Professional

Is the output of the flow one file with a section for each project or one file per project?  It sounds like its one section per project.  If that is the case I don't think you want the double loop where you are looping through the projects.  I think what you want at that point is a condition that checks each book to see if it is part of that project.  If it is add it, if it isn't skip it.  That should then be part of the overall project loop so you are appending each book list to the project as you are processing the project.

 

 



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

It is one file with a section for each project.  That's what I was trying to do - for each project, add only the titles that are part of that project.  Since the project titles and number of projects for each person are different, I need each of those to be dynamic. 

 

To check if a book is part of that project, I would need a list of all books in each project - but that is what I am trying to get.  Here's what I've tried:

 

condition1.pngcondition2.pngNow, this does remove the duplicates from my PDF, but still shows all book titles, even if they aren't part of the project.  It keeps appending the varProjBookList for all projects in the array.

 

I tried "is equal to" instead of "contains" but that didn't work at all.

 

I know it has to be the logic that I am using that is not producing what I am trying to get - but I'm not sure what I am missing?

 

Pstork1
Most Valuable Professional
Most Valuable Professional

If you want the list of books for each project in a section with each project then it has to be inside the loop where you are processing each project.  You can't do it in a separate place or the list won't appear at the right point in the file even if you do filter the list of books.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

That definitely helped a lot - moving the book list loop inside of where I was getting the other information.  The (hopefully) last issue I am having is that the variable will not clear after each round in the loop. 

 

varStays.png

 

I've tried using a condition to see if the variable is null (using the expression editor), checking if the length of the variable is = 0, and in each appending the variable to null (using the expression editor) - and then not using a condition but still appending the variable to be null, '',  "", or even a space.  I sue the compose step to see what is in the variable at various times - and this is happening

 

nullNotWorking.png

 

I've tried placing the append to null (blank, '', "") at various points in the flow (beginning, end, before appending) - it never quite works.

 

Is it possible to empty the string value between loops? Or is there another way to only show the active loop's data?

 

I truly appreciate all of your help - thank you!

 

Cheryl

Pstork1
Most Valuable Professional
Most Valuable Professional

The first actions in the outermost loop should be actions that set all the variables.  You can clear an array variable by using the set variable action and setting it to null.  You can clear a string variable with the same action and setting it to string('').

image.png



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Thank you very much, @Pstork1! That was the last piece I needed to finish this - I would not have been able to navigate all the piece to this without your assistance - thank you again!

Anonymous
Not applicable

Hi there - could you please post your final flow to help out those who are trying to solve the same challenge?

 

Thanks!

@CCava 

If you want to avoid designing / maintaining HTML, then you can use Encodian's Populate Word Document action which supports repeating sections within repeating sections.

The template is plain text so you can put a <<foreach>> tag within another <<foreach>> tag to achieve this. Overview of the template syntax is here: Template Syntax for Populate Word Document

 


Helpful resources

Announcements

Community will be READ ONLY July 16th, 5p PDT -July 22nd

Dear Community Members,   We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd.   During this period, members will not be able to Kudo, Comment, or Reply to any posts.   On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities.   What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!

Summer of Solutions | Week 4 Results | Winners will be posted on July 24th

We are excited to announce the Summer of Solutions Challenge!    This challenge is kicking off on Monday, June 17th and will run for (4) weeks.  The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions to as many questions as you can. Answers can be provided in all the communities.    Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”)   - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024   Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods.  You must enter into each weekly Entry Period separately.   How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different.    Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers.  At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing.  Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner.  A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random.  Individuals will be contacted before the announcement with the opportunity to claim or deny the prize.  Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners.   Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event.   ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE**   Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Community MembersNumber SolutionsSuper UsersNumber Solutions Deenuji 9 @NathanAlvares24  17 @Anil_g  7 @ManishSolanki  13 @eetuRobo  5 @David_MA  10 @VishnuReddy1997  5 @SpongYe  9JhonatanOB19932 (tie) @Nived_Nambiar  8 @maltie  2 (tie)   @PA-Noob  2 (tie)   @LukeMcG  2 (tie)   @tgut03  2 (tie)       Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 2: Community MembersSolutionsSuper UsersSolutionsPower Automate  @Deenuji  12@ManishSolanki 19 @Anil_g  10 @NathanAlvares24  17 @VishnuReddy1997  6 @Expiscornovus  10 @Tjan  5 @Nived_Nambiar  10 @eetuRobo  3 @SudeepGhatakNZ 8     Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 3:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji32ManishSolanki55VishnuReddy199724NathanAlvares2444Anil_g22SudeepGhatakNZ40eetuRobo18Nived_Nambiar28Tjan8David_MA22   Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 4:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji11FLMike31Sayan11ManishSolanki16VishnuReddy199710creativeopinion14Akshansh-Sharma3SudeepGhatakNZ7claudiovc2CFernandes5 misc2Nived_Nambiar5 Usernametwice232rzaneti5 eetuRobo2   Anil_g2   SharonS2  

Check Out | 2024 Release Wave 2 Plans for Microsoft Dynamics 365 and Microsoft Power Platform

On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners.    The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights.    Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.    

Updates to Transitions in the Power Platform Communities

We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for.  We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer.   Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together.   Here's to new beginnings and endless possibilities!   If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport.   To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages

Users online (1,205)