Using OpenXml with Power Automate Flows

By | October 8, 2020

Earlier this month, I wrote a couple of post about reporting/document generation. While working on it, I also tried one other option which you may find useful, too.

One of the biggest problems with Word Templates, be it classic word templates in model-driven apps or new populate word template action in Power Automate is that there are always limitations.

However, if there is a wish, there is a way.

Open XML SDK library has all we need to manipulate Word documents programmatically, so it’s entirely possible to even create custom templating functionality if we wanted to. Actually, I know some clients who did just that (to be honest, not sure if they did use Open XML).

In this post, I will walk you through the process of building an Azure Function which will be accepting two word documents, and which will merging those two into one.

Once there is an Azure Function, we can use it in a Flow like this:

image

When the flow runs, it will merge two files into one. Here is what it looks like:

  • You will see each individual file first
  • Then I’ll run the Flow
  • And, as a result, a new file will be created and downloaded. That file will have the content of the other two merged

How does it work?

There is an Azure Function that’s called in that HTTP Request action. This function would extract documents from the incoming HTTP request:

image

And there is a helper class that’s handling the merge:

image

You will find complete source code here:

https://github.com/ashlega/ItAintBoring.Functions

I struggled a little bit figuring out how to pass file content to the Azure Function, so the screenshot below might help – it turned out the trick was to use [‘$content’] parameter of the output:

image

But, realistically, does it solve the problem of generating large word documents out of individual files? Turned out not really – I tried with a hundred individual documents, and this whole thing just timed out. But, as a proof of concept… was an interesting exerciseSmile

One thought on “Using OpenXml with Power Automate Flows

  1. pawel

    Some alternate code for merging word documents (adding a page break between)

    //open for writing
    WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepathWrite, true);

    //open for reading
    WordprocessingDocument wordprocessingDocumentRead1 = WordprocessingDocument.Open(filepathRead, true);
    WordprocessingDocument wordprocessingDocumentRead2 = WordprocessingDocument.Open(filepathRead2, true);

    // Assign a reference to the existing document body.
    Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
    Body bodyRead1 = wordprocessingDocumentRead1.MainDocumentPart.Document.Body;
    Body bodyRead2 = wordprocessingDocumentRead2.MainDocumentPart.Document.Body;

    //Add new page break to the end of the document
    Paragraph para = body.AppendChild(new Paragraph());
    Run run = para.AppendChild(new Run());
    run.AppendChild(new Break() {Type = BreakValues.Page});

    //append the other document
    body.AppendChild(bodyRead2.CloneNode(true));

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *