page_type | urlFragment | products | languages | extensions | description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
word-import-template |
|
|
|
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
- Office connected to a Microsoft 365 subscription (including Office 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 |
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.
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.
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 -v
andnpm -v
in 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-proxy
To 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.
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.