cancel
Showing results for 
Search instead for 
Did you mean: 

Create DOCX Word document from template based on dynamic data in Microsoft Flow

This article demonstrates how to generate DOCX Word document from a template with the help of Microsoft Flow.

 

I will use actions from Plumsail Documents connector, which is a part of Plumsail Actions.

 

We will generate a hiring contract from a template. This is how our final Word file looks:

 

docx-sample-doc.png

 

Our template and result document has to be stored somewhere. Microsoft Flow has a lot of connectors for different systems. Here are just a few of them:

  • SharePoint
  • Salesforce
  • Box
  • OneDrive
  • Google Drive
  • Dropbox
  • SFTP
  • File System

You can store your source file anywhere. In this example, we will store our documents in SharePoint. Our Flow will get a template from a SharePoint document library, generate a new document based on this template and on some data. The resulting document will be stored back to SharePoint document library.

 

Firstly, we have to prepare the template file. Please follow this instruction to prepare your template.

 

Now we need to create a Microsoft Flow that will get the template from the SharePoint document library, apply data to this template and save result document back to the document library. This is how complete flow looks:

 

Select fields

 

Here is the step by step description for the flow.

 

Flow trigger

You can actually pick any trigger. For example, you can start Flow on file creation in a SharePoint document library. We use "Manually trigger a flow" trigger here to simplify the Flow.

 

Get file content

This action gets file content of the specified file from a SharePoint document library. You just specify SharePoint site URL and path to your file. We use this action to read .docx template.

You can use any other connector to get files from your system.

 

Create document from DOCX template

This is an action from Plumasail Documents connector, which is a part of Plumsail Actions.

There are two parameters:

  1. DOCX document content
  2. Template data

 

In the first parameter 'DOCX document content' we specified file content of a template from the output of the previous action. Use this link to download it.

 

In the second parameter, we specified data to apply to the template in JSON format. This is information about a sample employee. You can actually request this information from an external system with the help of another Microsoft Flow action.

 

This is our sample data:

{
    "EmployerFullName": "David Navarro",
    "EmployeeFullName": "Anil Mittal",
    "CompanyName": "Contoso LLC",
    "Position": "Marketing manager",
    "SalaryAmount": "5000",
    "ListOfBenefits": "list of any benefits that come with employment, including healthcare, retirement, gym membership, etc",
    "BonusesPolicyDescription": "annual evaluation",
    "EffectiveDate": "10/27/2017",
    "TerminationDate": "10/27/2018",
    "State": "New York"
}

 

You can find more information about this action here.

 

Create file

Once the result document is generated, we need to store the Word file somewhere. In our example, we use "Create file" action from SharePoint connector to store the document in SharePoint document library.

 

Select fields

 

You can use any other connector to store the Word document into your system.

 

This article was originally published here.

Comments

Nice, automation relate Word, SharePoint, Plumsail with Microsoft Flow.

It seems to create PDF file and send mail by store the Word file on OneDrive for Business, and convert it to PDF.

So this is interesting in terms of a static solution; however, when I try to extend it to generate a report based on JSON from a connector I can't seem to quite figure out the magic.  How does one get dynamic content for the Template Data section. 

 

In this example you are copying the JSON content directly into the Template Data field and I am able to get that to work.  When I try to use dynamic data from the output of a Microsoft Flow Action I am not having any luck.  As a case in point, I generate JSON from the SQL Server connector.  I try to reference it using the following:

 

body('Execute_stored_procedure')?['resultsets']?['Table1']?[0]?['Result']

 

The call fails with a Bad Request, Error 400, and the following error dump:

 

{
  "error": {
    "code"400,
    "source""msmanaged-na.azure-apim.net",
    "clientRequestId""1e448cad-bb22-4a39-8e6e-7db10eb8b9b0",
    "message""The response is not in a JSON format.",
    "innerError""Request is empty"
  }
}
 
The funny part is, it shows the JSON payload perfectly in the Template Data section of the error.  If you copy that exact data out an paste it in as static template data then all works perfectly so it isn't malformed JSON.  Any ideas on how to make it work?
Hi @wjhepworth, Please make sure that you have MergeFields in your template and you are using their name in the action. Then you could try to use the action like this, only instead use your dynamic content and your MergeField's name (please pay attention to ',' after the first string): https://i.gyazo.com/56c44d55a5f3503335d1d1512b1d68ac.png

Oleg,

Thank you for the response to this.  Unfortunately that isn't where I am not quite tracking.  Are you saying I should put in static JSON and treat it like a template itself?  Do you have any demos that show this?  I thought I went through almost everything on the Plumsail site but maybe I missed it.

 

The crazy part is seriously that I can copy the JSON from the error message Input section into the body and then it works.  It just seems Plumsakil HTML to Document is not recognizing that the payload it has in its possession is actually the payload.

 

Thank you,

 

-Bill

 

Hello @wjhepworth, Unfornutely, the action supports only one level JSON for now, but thanks to you we noticed this problem and it will be fixed on the next week.

Hi, thank you for you article, I think it is very useful. 

 

Unfortunately I've tried to replicate it and I've been unable to do it (Probably I'm too noob)

 

My problem is in Create document from DOCX template and the first parameter 'DOCX document content' .

When I tried to specified file content I can't . I get the message "We can't find any outputs to match this input format. Select "See more" to see all outputs from previous actions."

 

That happens to me with a custom DOCX template I've made and with the one you mentioned in the article. 

Do you have any idea what I'm doing wrong?

 

Thanks in advance

 

Hello @bilbox, Could you please try to click 'See more'? By doing this you should see more options for the input.
Meet Our Blog Authors
  • Working daily with Microsoft Cloud to deliver the needs of my company, my customers and various Microsoft communities and forums. | Office 365 | Flow | PowerShell | PowerApps | SharePoint |
  • Co-founder of https://plumsail.com, Office 365 and SharePoint expert. Passionate about design and development of easy to use, convenient and flexible products.
  • Microsoft Business Apps MVP. Owner of ThriveFast, an Office 365 consulting company.
  • 7x Microsoft Business Solutions MVP (CRM)
  • I'm keen in MS technologies, SharePoint, Office 365 and development for them
  • Daniel is a Business Productivity Consultant & Microsoft Business Solutions MVP who is very enthusiastic about all things Office 365, Microsoft Flow, PowerApps, Azure & SharePoint (Online). Since the preview, Daniel has been working with Microsoft Flow and later on with Microsoft PowerApps. That led to him being awarded an MVP Award for Business Solutions. He loves to blog, present and evangelize about improving productivity in the modern workspace with these amazing tools!
  • Michelle is an Office 365 solution architect in Twin Cities, MN. She has been delivering business collaboration solutions for years with her focus on SharePoint and Office 365. Michelle is a recent board member of the Minnesota Office 365 User Group and has been a member of the SharePoint community since 2009. She is a frequent speaker at MNSPUG and SharePoint Saturday and co-chaired the Legal SharePoint User Group for 4 years. Her most frequent projects have involved rolling out a large deployment of Office 365, SharePoint Online intranet, build of a "CHAMPS" Office 365 user adoption program and most recently, SharePoint On-Premise to Online Migration. Michelle is very excited about cloud technology as it is shifting her IT Pro focus to collaboration strategy and technical adoption.
  • I'm a Microsoft Office Servers and Services MVP with a special interest in SharePoint, Office 365, Microsoft Flow, Microsoft Teams and PowerApps. I work at Triad Group Plc ( https://triad.co.uk)
  • Passionate #Programmer #SharePoint #SPFx #Office365 #MSFlow | C-sharpCorner MVP | SharePoint StackOverflow, Github, PnP contributor