OfficeDev/Office-OOXML-EmbedAddin

Name: Office-OOXML-EmbedAddin

Owner: Office Developer

Description: Web application that uses Open XML SDK to configure Office documents to automatically open a specified add-in.

Created: 2017-06-13 17:42:04.0

Updated: 2017-07-13 23:00:23.0

Pushed: 2017-12-28 21:32:55.0

Homepage: null

Size: 20

Language: C#

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Web application that uses Open XML SDK to configure Office documents to automatically open a specified add-in

The are a number of scenarios in which an Office Add-in should be embedded in an Office document and automatically open when the document opens.

Note: For more information about automatically opening an add-in in a document, see Automatically open a task pane with a document.

This sample uses .NET and the Open XML SDK to configure Office documents with the markup that makes this possible. In this sample, the Script Lab add-in is embedded in an Office file of the user's choice.

Table of Contents
Change History

June 15, 2017:

Prerequisites
To use the project
  1. Clone or download this repo.

  2. Open the Office-OOXML-EmbedAddin.sln file in Visual Studio.

  3. In Visual Studio add references to these assemblies:

    • WindowsBase (in the Framework tab of the Visual Studio Extensions Manager)
    • DocumentFormat.OpenXml (in the Extensions tab)
  4. Press F5. This opens the home page of the application in your browser.

  5. Use the Browse control on the page to navigate to any Excel, Word, or PowerPoint file.

  6. Press Upload.

  7. Optional. If there is a particular snippet that you want to import as soon as Script Lab runs, enter its Gist ID in the textbox.

    Note: This feature may not be supported in Script Lab for a short period after this sample is released. But it will do no harm to enter a Gist ID.

  8. Press Embed Script Lab.

  9. Press Download and follow your browser's prompts to open or save the file. When the file opens, the task pane opens and near the top, you are prompted to trust Script Lab (unless you already had it installed). When you do, Script Lab opens.

  10. To verify that Script Lab is embedded in the document, close the file and reopen it. Script Lab opens in the task pane immediately.

About the code

The file Home.aspx.cs:

The file AddInEmbedder.cs:

The file OOXMLHelper.cs:

There are two critical lines in this file.

First, is the line in the GenerateWebExtensionPart1Content method that sets the reference to Script Lab (strictly speaking, to the manifest for Script Lab):

ebExtensionStoreReference webExtensionStoreReference1 = new We.WebExtensionStoreReference() { Id = "wa104380862", Version = "1.1.0.0", Store = "en-US", StoreType = "OMEX" };

In this code:

If you were setting up an add-in from a file share catalog for auto-open, you would use different values:

Note: For more information about alternative values for these attributes, see Automatically open a task pane with a document.

Second, is the line in the GeneratePartContent method that specifies the visibility of the taskpane when the file opens.

.WebExtensionTaskpane webExtensionTaskpane1 = new Wetp.WebExtensionTaskpane() { DockState = "right", Visibility = true, Width = 350D, Row = (UInt32Value)4U };

In this code, the Visibility property of the WebExtensionTaskpane object is set to true. The effect of this is that the very first time that the file is opened after the code is run, the task pane opens with Script Lab in it (after the user accepts the prompt to trust Script Lab). This is what we want for this sample. However, in most scenarios you will probably want this set to false. The effect of setting it to false is that the first time the file is opened, the user has to install the add-in, from the Add-in button on the ribbon. On every subsequent opening of the file, the taskpane with the add-in opens automatically.

The advantage of setting this property to false is that you can use the Office.js to turn give users the ability to turn on and off the auto-opening of the add-in. Specifically, your script sets the Office.AutoShowTaskpaneWithDocument document setting to true or false. However, if WebExtensionTaskpane.Visibility is set to true, there is no way for Office.js or, hence, your users to turn off the auto-opening of the add-in. Only editing the OOXML of the document can change WebExtensionTaskpane.Visibility to false.

Note: For more information about task pane visibility at the level of the Open XML that these .NET APIs represent, see Automatically open a task pane with a document.

Questions and comments

We'd love to get your feedback about this sample. You can send your feedback to us in the Issues section of this repository.

Questions about Microsoft Office 365 development in general should be posted to Stack Overflow. If your question is about the Office JavaScript APIs, make sure that your questions are tagged with [office-js].

Additional resources
Copyright

Copyright (c) 2017 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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.