[UPDATE] In which Application is my Add-in running?

UPDATE: The code outlined in this post will no longer work with Office Online. See this post for more information: http://stackoverflow.com/a/32851938/678505. Specifically, this block:

Update Dec 5, 2016: We will soon be releasing an API to detect the platform info (partially in response to the fact that the _host_info URL paramater, which folks had unofficially relied on, needed to be recently removed for Office Online). We also have a temporary workaround in anticipation of the forthcoming official API. See “In Excel Online, OfficeJS API is not passing the host_Info_ parameter anymore to Excel Add-In” for information on the API and workaround.

Original post:

In case you missed the news, there was an update to the Office API’s. Version 1.2 was announced at //build and released 2 weeks ago. This API enhancement gets us closer to parity with the native object models used in VBA and VSTO. One interesting aspect of the new Office add-ins (formally known as Apps for Office) is how you can use the same codebase across all the applications. However, as the API becomes richer they are also becoming more product specific. Meaning, there is now the Excel.run() and Word.run() commands (PowerPoint is a little lagging in this area, but it is supposedly on the horizon).  These allow you to do Excel Workbook (or Word document) context specific commands. And there is a lot of other goodies (like promises). But, I digress… wlEmoticon-hotsmile.png

I was working on a proof of concept for my customer when I found that I needed to use the same codebase for a lot of the same work, but in some cases I needed to do something specific in Word and in another, something specific in Excel. I came up with the following function that I placed in my App.js file:

var current;
app.hostTypes = { Word :"Word", PowerPoint :"PowerPoint", Excel :"Excel"};
app.getHost = function () {
	if (current == null) {
		if (Office.context.requirements.isSetSupported('WordApi')) {
			current = app.hostTypes.Word;
		} else if (Office.context.requirements.isSetSupported('ExcelApi')) {
			current = app.hostTypes.Excel;
		} else {
			var host = $.urlParam("_host_Info");
			if (host.toLowerCase().indexOf("word",0) >= 0) {
				current = app.hostTypes.Word;
			} else if (host.toLowerCase().indexOf("excel",0) >= 0) {
				current = app.hostTypes.Excel;
			} else {
				current = app.hostTypes.PowerPoint;
		return current;
	else {
		return current;

To use this you would do this for Word:

            /// WORD
            if (app.getHost() == app.hostTypes.Word) {
                // Word specific code where

Or, this for Excel:

            /// EXCEL
            if (app.getHost() == app.hostTypes.Excel) {
                // Excel specific code here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s