Determining the host application of a shared add-in?

A

andreas.baus

What is the best (most reliable) way to determine which Office
application is hosting an shared addin? In other words, in the
OnConnection method, I just get a reference to a generic object, but if
the addin is supposed to be able to work with either (for example)
Word, Excel or PowerPoint, how do I figure out which of them loaded it
so I can branch to application-specific code wherever necessary?

I tried force casting the object to Word.Application, Excel.Application
etc., catching exceptions until I find a cast that doesn't fail, but
that has the unpleasant side effect that for each failed cast, the
loading of the host application is delayed a small, but noticeable
amount of time. Surely there must be a more elgant way? Thanks in
advance to anyone who can give me some ideas.
 
Y

Yi

It is easy. I do the following cast and it works fine.

m_WordApp = CType(application, Word.Application)
If (m_WordApp Is Nothing) Then
is_wordapp = False
Else
is_wordapp = True
End If
 
B

Bernd

Hi Andreas,

you can get the type of the hosting application as follows:

If TypeOf Application Is Word.Application Then
....
ElseIf TypeOf Application Is Excel.Application Then
....
EndIf

Regards
Bernd
 
A

andreas.baus

Thanks for the answers. In case anyone is interested, the solution I
came up with in the end looks like this: (I'm using C#)

public class Connect : Object, Extensibility.IDTExtensibility2
{
// variables for references to the host application
// using the proper type instead of generic object
// saves a lot of typecasting later on
private Word.Application wordApp;
private Excel.Application excelApp;
private PowerPoint.Application pptApp;

// enum & variable for determining whether the addin has
// successfully identified the host application, and which one
private enum HostApplications
{
Unknown,
Word,
Excel,
PowerPoint
}
private HostApplications whichHost;

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
whichHost = HostApplications.Unknown;

// now try to cast the application object to the possible
// specific types; the 'as' operator doesn't throw an exception
// when the cast fails, it simply returns null.
if ((wordApp = application as Word.Application) != null)
{
whichHost = HostApplications.Word;
}
else if ((excelApp = application as Excel.Application) != null)
{
whichHost = HostApplications.Excel;
}
else if ((pptApp = application as PowerPoint.Application) != null)
{
whichHost = HostApplications.PowerPoint;
}

// rest of the code omitted

}
}

Apparently, the exception thrown by trying a forced cast to an
incompatible type (with the normal "wordApp =
(Word.Application)application;" syntax) was what caused the delays; the
'as' operator behaves much more gracefully.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top