How to get the application object type

D

David Thielen

Hi;

We have the following code for our initial connection:

public void OnConnection(object application, ext_ConnectMode
connectMode, object addInInst, ref Array custom)
{
try
{
wordApp = application as
Microsoft.Office.Interop.Word.Application;
excelApp = application as
Microsoft.Office.Interop.Excel.Application;
....

However, sometimes those both return null when starting Excel (usually
when clicking on an Excel file which loads Excel for the file). We
tried looking at the type of application but it is System.__ComObject
which doesn't tell us anything.

How can we figure out what is going on?

thanks - dave


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
S

Scott McPhillips [MVP]

If you're trying to find out what application loaded you have a look at
Application.Name
 
J

Ji Zhou [MSFT]

Hello Dave,

Thanks for using Microsoft Newsgroup Support Service! My name is Ji Zhou
[MSFT] and I will be working on this issue with you.

First, I want to make sure that I understand your issue correctly. From
your description, when writing a Shared Add-In, we can get the parameter
"application" well in OnConnection function, and it is System.__COMObject
type. But, after we convert it into Excel.Application, excelApp returns
null. This usually happens when double-clicking an Excel workbook to load
Excel Applications, and does not happen when opening a Word Application.

I tried to reproduce this issue, but without success. The following is what
I have done:
1. I created a Shared Add-in, and added references to Excel and Word 2007
PIA.
2. Added the following codes into OnConnection() function.
try
{
wordApp = application as Microsoft.Office.Interop.Word.Application;
excelApp = application as Microsoft.Office.Interop.Excel.Application;
if (excelApp == null)
{
MessageBox.Show("ExcelApp is NULL");
}
else
{
MessageBox.Show("ExcelApp is not NULL");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
3. I built my Shared Add-In, and prepared to install it on different client
machines to carry out tests in next steps.
4. Installed it to a client machine that has Excel 2007 installed. I double
clicked workbook.xlsx file in my disk to open the Excel application. A
dialog always pops up saying "ExcelApp is not null"
5. Installed it to a client machine that has Excel 2003 and Office
Compatibility Pack installed. Coped an Excel 2007 file workbook.xlsx to a
client machine and double clicked that file. It still indicates that
"ExcelApp is not null"
6. Repeated the first step, and added a reference to Excel and Word 2003
PIA to test, compiled the setup project.
7. Installed 2003 PIA version to a client machine with Excel 2007
installed. Double clicked a file, ExcelApp is not null.
8. Installed 2003 PIA version to a client machine with Excel 2003
installed. Double clicked a file, ExcelApp is not null.

My thoughts on troubleshooting the problem is, firstly we can use
Microsoft.VisualBasic.Information.TypeName() function to get the exact type
name of a COM object instead of getting "System.__COMObject". The function
can only tell if application object is an "Application" type object, rather
than telling us whether it is Word.Application or Excel.Application. So, if
Microsoft.VisualBasic.Information.TypeName(application) returns
"Application", we can try to use late binding to get Application’s Name
property to see if it is "Microsoft Excel". If yes, we convert it to the
Excel.Application type. Codes are like: (To use
Microsoft.VisualBasic.Information.TypeName() function in C#, we need to
manually import Microsoft.VisualBasic assembly)

public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
try
{
if (Microsoft.VisualBasic.Information.TypeName(application) ==
"Application")
{
String appName = application.GetType().InvokeMember("Name",
System.Reflection.BindingFlags.GetProperty,
null, application, null) as String;

if (appName == "Microsoft Word")
{
wordApp = application as Word.Application;
if (wordApp != null)
{
MessageBox.Show("application is Word app");
}
else
{
MessageBox.Show("wordApp is null");
}
}
else if (appName == "Microsoft Excel")
{
excelApp = application as Excel.Application;
if (excelApp != null)
{
MessageBox.Show("application is Excel app");
}
else
{
MessageBox.Show("excelApp is null");
}
}
else
{
MessageBox.Show("application is neither Word nor Excel");
}
}
else
{
MessageBox.Show("application is not an Office app");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

If the above information does not help to narrow down the problem, could
you please post the steps in detail that reproduce the issue? I will
perform more researches on my side.

Best Regards,
Ji Zhou ([email protected], remove ‘online.’)
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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