Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the Office AddIn Host it is a Word application or Excel using office.js?

I am creating a office AddIn which works in both excel and word applications and based on host if it is a word or excel host i want to execute different logic. I am using office.js to create office Addin.

for example :-

let say type="Excel" // after some logic executed 

if(type=="Excel")
 {
//run code for excel applications 
}
else
{
//run code for word applications
}

I have tried to use the bellow:-

 if (Office.context.requirements.isSetSupported('ExcelApi', '1.1')) {
            alert("yes it is excel");
        }

but it is not working when i run it in excel.

I have sent the host in manifest file also

 <Hosts>
     <Host Name="Document" />
    <Host Name="Workbook" />
  </Hosts>

also I got some code alter doing a lot of googling I found the bellow code which is not working for me

function getHostInfo() {
    var _requirements = Office.context.requirements;
    var types = ['Excel', 'Word'];
    var minVersions = ['1.1', '1.0']; // Start with the highest version

    // Loop through types and minVersions
    for (var type in types) {
        for (var minVersion in minVersions) {

            // Append "Api" to the type for set name, i.e. "ExcelApi" or "WordApi"
            if (_requirements.isSetSupported(types[type] + 'Api', minVersions[minVersion])) {
                return {
                    type: types[type],
                    apiVersion: minVersions[minVersion]
                }
            }
        }
    }
};

Thank you

like image 874
Pradeep Gaba Avatar asked Jun 13 '16 08:06

Pradeep Gaba


People also ask

What office add-ins?

You can use the Office Add-ins platform to build solutions that extend Office applications and interact with content in Office documents. With Office Add-ins, you can use familiar web technologies such as HTML, CSS, and JavaScript to extend and interact with Outlook, Excel, Word, PowerPoint, OneNote, and Project.


1 Answers

Update Dec 5, 2016: We will soon be releasing an API to detect the host and platform information (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 more info.

Note that for many light-up scenarios, you would still be better off using API Set detection. See "Neat ways to get environment (i.e. Office version)" for more information on requirement sets.


The if (Office.context.requirements.isSetSupported('ExcelApi', '1.1')) should work for you, IF you are in Excel 2016. It will not work (i.e., return false) in 2013.

If you are targeting Office 2013, and need a solution just for Word & Excel, you can use the ability to write OpenXML as a distinguishing factor (Word can, Excel can't). So check for Office.context.requirements.isSetSupported('OoxmlCoercion'). It will return true for Word, false for Excel.

like image 177
Michael Zlatkovsky - Microsoft Avatar answered Oct 20 '22 19:10

Michael Zlatkovsky - Microsoft