Skip to content

Latest commit

 

History

History

word-import-template

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
page_type urlFragment products languages extensions description
sample
word-import-template
office-word
office
javascript
contentType technologies createdDate
samples
Add-ins
03/08/2024 4:00:00 PM
Shows how to import templates in a Word document.

Import templates in a Word document

Summary

This sample shows how to import a Word document template with an add-in.

Description

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.

Import template add-in task pane.

Applies to

  • Word on Windows
  • Word on Mac
  • Word on the web

Prerequisites

  • Office connected to a Microsoft 365 subscription (including Office on the web).

Solution

Solution Authors
How to import a template in a Word document Microsoft

Version history

Version Date Comments
1.0 03-08-2024 Initial release

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.

  1. Download the manifest.xml file from this sample to a folder on your computer.
  2. Sideload the add-in manifest in Word by following the appropriate instructions in the article Sideload an Office Add-in for testing.

Try it out

Once the add-in is loaded, use the following steps to try out the functionality.

  1. Open Word on Windows, on Mac, or in a browser.

  2. To open the add-in task pane, go to the Home tab and choose Show Task Pane.

  3. 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.

    The initial screen displaying the button to choose a file.

    For convenience, the resources folder of this project includes a template example.docx file.

    Screen showing the imported template.

  4. In the document, update the text and other content.

Run the sample from localhost

If you prefer to configure a web server and host the add-in's web files from your computer, use the following steps.

  1. 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 and npm -v in your terminal.

  2. 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
  3. 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.

  4. From a command prompt, go to the root folder and run the following command.

    http-server -S --cors . -p 3000
  5. 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
  6. Sideload manifest-localhost.xml in Word by following the appropriate instructions in the article Sideload an Office Add-in for testing.

  7. Try out the sample!

Make it yours

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.

Related content

Questions and feedback

  • 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

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.