| page_type | sample | |||||||
|---|---|---|---|---|---|---|---|---|
| urlFragment | word-import-template | |||||||
| products |
|
|||||||
| languages |
|
|||||||
| extensions |
|
|||||||
| description | Shows how to import templates in a Word document. |
This sample shows how to import a Word document template with an add-in.
The user updates their Word document with the content from another Word document, treating the external document like a template. The user selects a Word document through the add-in UI then it's applied to the current document.
- Word on Windows
- Word on Mac
- Word on the web
| Solution | Authors |
|---|---|
| How to import a template in a Word document | Microsoft |
| Version | Date | Comments |
|---|---|---|
| 1.0 | 03-08-2024 | Initial release |
| 1.1 | 07-07-2025 | Add support for the unified manifest for Microsoft 365 |
- Add-in only manifest
- To run the add-in only manifest, which is the manifest.xml file in the sample's root directory Samples/word-import-template, go to the Add-in only manifest section.
- Unified manifest for Microsoft 365
- To run the unified manifest for Microsoft 365 (manifest.json), go to the Unified manifest section.
Use one of the following add-in file hosting options to run the sample.
You can run this sample in Word on Windows, on Mac, or in a browser. The add-in web files are served from this repo on GitHub.
- Download the manifest.xml file from this sample to a folder on your computer.
- Sideload the add-in manifest in Word by following the appropriate instructions in the article Sideload an Office Add-in for testing.
- Follow the steps in Try it out to test the sample.
If you prefer to configure a web server and host the add-in's web files from your computer, use the following steps.
-
Install a recent version of npm and Node.js on your computer. To verify if you've already installed these tools, run the commands
node -vandnpm -vin your terminal. -
You need http-server to run the local web server. If you haven't installed this yet, run the following command.
npm install --global http-server -
Use a tool such as openssl to generate a self-signed certificate that you can use for the web server. Move the cert.pem and key.pem files to the root folder for this sample.
-
From a command prompt, go to the root folder and run the following command.
http-server -S --cors . -p 3000 -
To reroute to localhost, run office-addin-https-reverse-proxy. If you haven't installed this, run the following command.
npm install --global office-addin-https-reverse-proxyTo reroute, run the following in another command prompt.
office-addin-https-reverse-proxy --url http://localhost:3000 -
Sideload manifest-localhost.xml in Word by following the appropriate instructions in the article Sideload an Office Add-in for testing.
-
Follow the steps in Try it out to test the sample.
-
If you want to run the web server on localhost, install a recent version of npm and Node.js on your computer. To check if you've already installed these tools, from a command prompt, run the following commands.
node -v npm -v
-
If you want to run the sample using GitHub as the web host, install the Microsoft 365 Agents Toolkit command line interface (CLI). From a command prompt, run the following command.
npm install -g @microsoft/teamsapp-cli
You can run this sample in Word on Windows, on Mac, or in a browser. Use one of the following add-in file hosting options.
The quickest way to run the sample is to use GitHub as the web host. However, you can't debug or change the source code. The add-in web files are served from this GitHub repository.
- Download the manifest-configurations/unified/word-import-template.zip file from this sample to a folder on your computer.
- Sideload the add-in manifest in Word by following the appropriate instructions in the article Sideload Office Add-ins that use the unified manifest for Microsoft 365.
- Follow the steps in Try it out to test the sample.
If you prefer to host the web server on localhost, follow these steps:
-
Clone or download this repository.
-
From a command prompt, go to the root of the project folder /Samples/word-import-template.
-
Copy the files from the manifest-configurations/unified subfolder to the root folder.
-
Run the following commands.
npm installnpm startThis starts the web server on localhost and sideloads the manifest.json file to Word.
-
Follow the steps in Try it out to test the sample.
-
To stop the web server and uninstall the add-in from Word, run the following command.
npm stop
Once the add-in is loaded, use the following steps to try out the functionality.
-
Open Word on Windows, on Mac, or in a browser.
-
To open the add-in task pane, go to the Home tab and choose Show Task Pane.
-
In the "Template" section of the add-in UI, select Choose File. Navigate to the location of your .docx file then open the file. The template is automatically applied to your document, replacing any preexisting content.
For convenience, the resources folder of this project includes a template example.docx file.
-
In the document, update the text and other content.
The following are a few suggestions for how you could tailor this to your scenario.
- Include single sign-on (SSO) to support managing sessions and persisting settings for the user.
- Provide personalized or company-approved templates for users to access.
- Enable users to personalize templates and save to shared location.
- Did you experience any problems with the sample? Create an issue and we'll help you out.
- We'd love to get your feedback about this sample. Go to our Office samples survey to give feedback and suggest improvements.
- For general questions about developing Office Add-ins, go to Microsoft Q&A using the office-js-dev tag.
Copyright (c) 2024 Microsoft Corporation. All rights reserved.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.


