Friday, November 6, 2015

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

This is a three-part blog series to introduce Word Add-in and how to use Open XML SDK for JavaScript. This is the last post of this three-part blog series on Office Word add-ins: Insert template base on Word is a resources post.

Resources

The two first links is for the openxmldeveloper.org and where to download the code.
- openxmldeveloper - http://openxmldeveloper.org/
- mdownload - https://openxmlsdkjs.codeplex.com/

The third link is to a snippet collection with sample for the build in Word add-in https://officesnippetexplorer.azurewebsites.net/#/snippets/word

The fourth link is to the MSDN section “JavaScript API for Office”, where everything about the API for Office Add-ins can be found. https://msdn.microsoft.com/en-us/library/office/fp142185.aspx

Related blog posts

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




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




Wednesday, November 4, 2015

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

This is a three-part blog series to introduce Word Add-in and how to use Open XML SDK for JavaScript. The goal of these blog posts is to create an Office add-in that can insert a Word base template into another Word file. By describe how to create a Word add-in that can read a Word file and use the file as a template. This allow an user to choice and insert a prepare template and the templates is easy created in Word.

User case:

  1. An user starts Word with the add-in.
  2. The user presses a button in the add-in.
  3. The add-in call a services for the template.
  4. The add-in insert the template in the current Word file.

Steps, Create the project and setup API services to return a template:

  1. Create a Word add-in project base on the “App for Office”.clip_image002clip_image004clip_image006
  2. Add a WebAPI webservice to the project.
    Add a Web API Controller Class to the project. N.B.! remember to post-fixet the filename with Controller!
    clip_image008Edit the ApiController to return a base64 string.
    This example read a docx file from a sub folder “Documetns” in the website that host the Add-in.
    clip_image010Add a Global.asax to the project to allow ApiController to be start and call.
    clip_image012

    clip_image013
    Add the folder “Documents” file to the project and create a Word file named template1.docx. And the project structure and Word file shout look something like this.clip_image015

Related blog posts