How to generate attachments with Office files in Power Apps / Power Platform
This tutorial shows how to generate Office files (Word and Excel) from data in a Canvas App and attach them to an item in SharePoint using Power Apps and Power Automate. It is useful for creating reports or custom labels automatically without leaving the Power Platform.
Prerequisites
- Account with Power Apps / Power Automate licensing and access to SharePoint Online
- A SharePoint site and a list prepared to receive attachments
- Basic knowledge of Canvas Apps and creating Flows in Power Automate
Step 1: Prepare the Office file template
We will use a Word template (docx) with simple content placeholders (Quick Parts or text between braces) that will be replaced by the Flow. For Excel, prepare a sheet with a named range that the Flow can fill via Office Scripts or populate a CSV file if you prefer simplicity.
Simple example for Word: create a Word file with text like {{Nome}} and {{Data}} and save it in the templates document library in a SharePoint library called Templates.
Step 2: Create the Canvas App that collects data
In Power Apps create a Canvas App (tablet/phone) that collects the fields you want to include in the file. Add a button for “Generate and Attach” that calls the Flow.
// Example OnSelect for the button
// assumes TextInputNome and DatePickerData on the screen
Set(varNome, TextInputNome.Text);
Set(varData, Text(DatePickerData.SelectedDate, "yyyy-MM-dd"));
'GerarFicheiroFlow'.Run(varNome, varData, ThisItem.ID)
Step 3: Create the Power Automate Flow triggered by Power Apps
Create an "Instant cloud flow" triggered by Power Apps. It will receive parameters (for example Nome, Data, ItemID) and create the filled file from the template, then attach it to the SharePoint list item.
Steps in the Flow:
- Trigger: Power Apps (V2) — add input parameters (Nome, Data, ItemID).
- Get template file: SharePoint > Get file content (path) — point to /Templates/template.docx.
- Replace text in the document: there are two options:
- Use the Word Online (Business) connector > Populate a Microsoft Word template — if the template uses Content Controls.
- Use a Compose action and replace strings in the base64 file (more advanced) — not recommended for beginners.
- Create the filled file: SharePoint > Create file — in the desired library (for example Documents), use the output of the previous step as Content.
- Get the created file: Get file properties / Get file content to retrieve the content if needed.
- Attach to the SharePoint list: SharePoint > Add attachment — site + list name + Id (ItemID received from Power Apps) + FileName and File Content (using the output of the created file).
- Respond to Power Apps (optional): Response to Power Apps with success/file URL for feedback in the App.
// Useful example expressions in the Flow
// To turn text into a filename
concat('Relatorio_', replace(triggerBody()['Nome'],' ','_'), '_', utcNow(), '.docx')
// If using Get file content, the content goes directly to Create file
body('Get_file_content')
Step 4: Configure the Populate a Microsoft Word template action
If you choose to use Word Online (Business) > Populate a Microsoft Word template, open your template in Word and convert the replacement points into Content Controls (Developer > Controls). After uploading the template in the action, the connector exposes those fields to fill with the values received from Power Apps.
// In the Flow, map Power Apps parameters to the template fields
Nome: triggerBody()['text_Nome']
Data: triggerBody()['text_Data']
Step 5: Connect the Flow to Power Apps and handle responses
In the final action of the Flow, use the "Respond to Power Apps" action to return the result (for example the file URL or a status). In the Canvas App, capture the Flow return and show a confirmation to the user.
// In Power Apps, handle response (simplified example)
Set(resp, 'GerarFicheiroFlow'.Run(varNome, varData, ThisItem.ID));
If(resp.Status = "OK", Notify("File generated and attached."), Notify("Error generating file", NotificationType.Error))
Verify the result
To confirm everything works: a) in Power Apps enter data and click "Generate and Attach"; b) check the SharePoint list on the corresponding item to see if the attachment is present; c) open the generated file to validate the substituted values. If it does not appear, check the Flow run history for errors (inputs, permissions, or file paths).
Conclusion
With this process you can generate custom Word/Excel files and automatically attach them to items in SharePoint directly from a Canvas App, simplifying reporting and communications. Next steps: add version control, generate multiple attachments, or use Office Scripts for advanced Excel manipulation. Tip: if you encounter errors check SharePoint permissions and whether the template uses compatible Content Controls.