Thursday, November 5, 2015

Office Word add-ins: Insert template base on Word, Part 2

This is the second post of this three-part blog series to introduce Word Add-in and how to use Open XML SDK for JavaScript. This post describe what framework I use to read whole Word file and interact with the current Word file.

Read the first part here http://blog.andersdissing.com/2015/11/office-word-add-ins-insert-template-part1.html

Add JavaScript frameworks to the Word Add-in

To interact with an Office document, I use the JavaScript API that comes with the Office add-in. But read parts or the whole document, from the API service, I use the Open XML SDK for JavaScript. This API has the ability to create a JavaScript object base on a base64 string and the notion of Word packages. This allows the JavaScript FlatOpc object to insert to the Word file.

Ressources:

OpenXML Developer - http://openxmldeveloper.org/ Download - https://openxmlsdkjs.codeplex.com/

Steps, add OpenXmlSDK, retrieve and insert template:

  1. First we need to download and add the openxmlsdkjs to the project.
    Before:
    clip_image001
    After:
    clip_image002 
  2. Modify the Home.html, head html tag, to include the openxml.js and dependence.
    Before:
    clip_image004
    After:
    clip_image006 
  3. Modify the Home.html, body html tag, to include a button
    clip_image007 
  4. Modify the Home.js
    - Bind the click event to a function
    - Create the click event function
    clip_image008 
  5. Fill in the logic of the insertTemplate function
    - Get template (base 64 string) from API service
    - Create a openXml.OpenXmlPackage object
    - Save the OpenXmlPackage to a FlacOpt object
    - Use the build-in Word API to insert the FlacOpt object to the body of the Word file
    clip_image010 
  6. The test
    - F5 to run the project
    - Click the ‘insert template’ in the Add-in. clip_image012

Related blog posts

Read the first part of the Office Word add-ins: Insert template base on Word here http://blog.andersdissing.com/2015/11/office-word-add-ins-insert-template-part1.html




3 comments:

  1. Does this also works with headers and sections etc.

    ReplyDelete
  2. Hi Nick
    I convinced it is possible but not with this example

    ReplyDelete
  3. Your website is really cool and this is a great inspiring article. Satta king 786

    ReplyDelete