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.

Can you use this to update an existing SharePoint document by saving the document created to the same location? I'm thinking to use this technique to create a new document based off an update in a PowerApps, and then save the file over the existing file. 

 

I currently have the URL for the document in the SharePoint list, but it's also part of the document library on the SharePoint site. I'm not sure if the easiest thing to do would be to draw the URL from the list to save the new version so there is a unique link. I'm not sure if this would change the name of the file, or just update the content.

There may also be an alternative way to access the file using the document library, but the current list doesn't store the file name, so I'm not sure how to link the flow to the document library. 

 

Anyone tried this before and have advice/suggestions on the best way to go with this?

 

Meet Our Blog Authors
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Cambridge UK Power Platform User Group Leader, Technical evangelist and speaker. Always says yes to coffee! #LetsGetCoffee
  • Passionate #Programmer #SharePoint #SPFx #Office365 #MSFlow | C-sharpCorner MVP | SharePoint StackOverflow, Github, PnP contributor
  • I am building business processes and applications that are easy for users' to stick to, so they can follow and understand them. In overall I transform processes to be more reliable and effortless. I am a proud co-organizer of SharePoint Saturday Warsaw and active community member, blogger and international speaker.