7

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

Pradeep Gaba
  • 415
  • 3
  • 17

3 Answers3

1

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.

Community
  • 1
  • 1
  • Hi @Michael will the code Office.context.requirements.isSetSupported('OoxmlCoercion') work on Office 2016 as well ? I am unable to check as I don't have office 2016 installed on my machine. thanks. – Pradeep Gaba Jun 14 '16 at 10:56
  • 1
    Yes, it will work on all platforms/versions where Office Add-ins are supported. – Michael Zlatkovsky - Microsoft Jun 14 '16 at 16:01
  • Hi @Michael Zlatkovsky, Sorry to say but the code that you said it will work it is not working today I was doing testing of my addin and I had written the logic as per the host application but it is not working please look in to this. function someFunction(obj) { //check for the host is excel or Word if (Office.context.requirements.isSetSupported('OoxmlCoercion')) { //logic when it is word host } else { //do someting when it is excel } – Pradeep Gaba Jun 20 '16 at 11:45
  • Are you referencing the CDN version of Office.js? – Michael Zlatkovsky - Microsoft Jun 20 '16 at 15:40
  • 1
    Could you try the CDN location? And/or updating your Office.js Nuget package to latest? – Michael Zlatkovsky - Microsoft Jun 20 '16 at 17:04
  • I have tried the Cdn's "https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js" and also " also I tried "https://appsforoffice.microsoft.com/lib/1/hosted/Office.js" but none of them worked. – Pradeep Gaba Jun 20 '16 at 17:24
  • Very strange. Could you describe what versions of Office you're testing on (desktop or online, what version # if desktop, and what version of online -- onedrive vs. onedrive for business, vs on-prem sharepoint?) – Michael Zlatkovsky - Microsoft Jun 20 '16 at 21:10
  • It is office 2013 desktop version. I tested the code after i created a demo application for Word Addin and it is working there but not in my code where i need to work on mixed environment with AddIn in word and excel both with the same addIn – Pradeep Gaba Jun 21 '16 at 07:55
  • @MichaelZlatkovsky The office.js API are envolving. It is so vague to determine a host by function sets or functions which may exist tomorrow. Please provide a method to excactly determine the host! (All the other stuff may of course done by set or function checks!) – ndee Jun 21 '16 at 14:13
  • @ndee, see my comment at http://stackoverflow.com/questions/32840459/neat-ways-to-get-environment-i-e-office-version/32851938?noredirect=1#comment63341689_32851938. For 2013, you sort of have to live with what has shipped (though FWIW, those APIs by definition are static in time). For 2016+, you can always find out the host by querying `Office.context.requirements.isSetSupported("ExcelApi")`, or `WordApi`, etc. – Michael Zlatkovsky - Microsoft Jun 21 '16 at 14:42
  • @PradeepGaba, I'm not sure what you mean / what your question is. If you are able to make the code work in a demo application, what is different about your other application? – Michael Zlatkovsky - Microsoft Jun 21 '16 at 14:43
  • @MichaelZlatkovsky in addIn manifest i have written the bellow code which actually a address of a mvc application in which all the logic resides and I am hosting this mcv application in the add in and this mvc application is using office.js which is working fine. but only in this case were i need to determine the host it is not working. today i created a word addIn as a demo and i replaced the demo addin manifest the above code but still condition goes false :( – Pradeep Gaba Jun 23 '16 at 07:01
  • @MichaelZlatkovsky if I am creating a new project the logic you said is working but not when i replaced the manifest code with the bellow one – Pradeep Gaba Jun 23 '16 at 07:03
  • Are you sure that the code you're running on localhost:123456 is doing the right thing? In particular, is it pointing at the CDN? And does it have Office.initialize? Again, sample code (or link to zipped-up as-simple-as-possible project) would help. – Michael Zlatkovsky - Microsoft Jun 23 '16 at 17:06
  • It is working fine now .It was my mistake i was pointing to wrong cdn. thank you. – Pradeep Gaba Jul 05 '16 at 07:58
  • @MichaelZlatkovsky do you have any idea what is is the official release date of https://github.com/OfficeDev/office-js-docs/tree/WordJs_1.3_Openspec/word which is currently a preview version. – Pradeep Gaba Jul 05 '16 at 07:59
  • Almost all of the code for 1.3 will be available in the "June" update to Office 2016, which usually gets released within a few weeks of the month that it's named after (so in this case, a few weeks from today). Once it's out, we'll release the new Office.js on the beta CDN, and update it again next month when the rest of 1.3 is finished. And from Beta to Prod is usually a couple of months (though you can certainly develop agains the beta). – Michael Zlatkovsky - Microsoft Jul 06 '16 at 00:23
  • @PradeepGaba, see update to my answer, regarding detecting host and platform. – Michael Zlatkovsky - Microsoft Dec 05 '16 at 19:35
  • Thanks @MichaelZlatkovsky – Pradeep Gaba Dec 06 '16 at 12:52
0

You can always inspect the location.search object - should return a string like ?_host_Info=Word|Win32|16.01|en-US.

KernelPanic
  • 2,328
  • 7
  • 47
  • 90
foilage
  • 56
  • 5
  • 1
    we are evaluating adding an API to detect host ("Excel, Word, Outlook, etc) & platform (Desktop, Mac, Web, etc) info. To make sure we're taking it into account, would you mind describing the particular use-cases you have that led you to seek out _host_Info? Thanks! – Michael Zlatkovsky - Microsoft Sep 26 '16 at 14:08
0

Note the upcoming API that provides a formal API to get such information.

Specification link: https://github.com/OfficeDev/office-js-docs/tree/ContextAdditions_OpenSpec

This is a better way to get the information you are looking for rather than relying on URL query string or session storage. Such methods are bit risky as underlying behavior could change without warning. It is always a good option to use published APIs.

Sudhi Ramamurthy
  • 2,358
  • 1
  • 10
  • 14