C# Excel add-in: Why need InvokeMember to get Application property?

R

Russell Smith

I am creating an Excel add-in. Please note project type is Shared Add-in
vice Excel Workbook. In the OnStartupComplete event handler I am adding a
new menu item. I start by getting a reference to the Tools menu. This
works fine.

Type applicationType = applicationObject.GetType();
CommandBars bars = (CommandBars)applicationType.InvokeMember(
"CommandBars", BindingFlags.GetProperty, null, applicationObject,
null);
CommandBar bar = bars["Tools"];
Debug.WriteLine(bar.Name);

But the following more readable code throws "An unhandled exception of type
'System.ExecutionEngineException' occurred in testaddin.dll" on the third
line.

Excel.Application app = (Excel.Application)applicationObject;
CommandBars bars = app.CommandBars;
CommandBar bar = bars["Tools"];
Debug.WriteLine(bar.Name);

I have read Programming Office Applications Using Microsoft Visual C#
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/OffCSharp.asp)
and the potential problem I see is "Handling Parameterized Properties with
Excel". But that doesn't seem applicable or the first version wouldn't work
correctly. What gives?
 
N

Nicholas Paldino [.NET/C# MVP]

Russel,

The only thing I can think of is that the version of excel that you are
testing your add in against is different than the version that the interop
library was generated for. Is this possible?
 
R

Russell Smith

I checked references and worked around the problem but am not sure whether
this is the right approach. The default Shared Add-in project contains a
reference named "Office".

Description: <blank>
Identity: Office
Path: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Office.dll

I noticed that the default VSTO Excel Workbook project contains a reference
named "Microsoft.Office.Core".

Description: Microsoft Office 11.0 Object Library
Identity: {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}\2.3\0\primary
Path:
C:\WINDOWS\assembly\GAC\Office\11.0.0.0__71e9bce111e9429c\Office.dll

When I deleted the Add-In project's Office reference and replaced it with
the same reference used by the Excel Workbook project, it worked fine. I
have no idea which is the correct reference to use. Based on the DLL file
timestamps, the first was installed as part of Office 2003, the second was
installed as part of VSTO.

Please enlighten me.

V/R
Russell

Nicholas Paldino said:
Russel,

The only thing I can think of is that the version of excel that you are
testing your add in against is different than the version that the interop
library was generated for. Is this possible?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Russell Smith said:
I am creating an Excel add-in. Please note project type is Shared Add-in
vice Excel Workbook. In the OnStartupComplete event handler I am adding a
new menu item. I start by getting a reference to the Tools menu. This
works fine.

Type applicationType = applicationObject.GetType();
CommandBars bars = (CommandBars)applicationType.InvokeMember(
"CommandBars", BindingFlags.GetProperty, null, applicationObject,
null);
CommandBar bar = bars["Tools"];
Debug.WriteLine(bar.Name);

But the following more readable code throws "An unhandled exception of
type 'System.ExecutionEngineException' occurred in testaddin.dll" on the
third line.

Excel.Application app = (Excel.Application)applicationObject;
CommandBars bars = app.CommandBars;
CommandBar bar = bars["Tools"];
Debug.WriteLine(bar.Name);

I have read Programming Office Applications Using Microsoft Visual C#
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/OffCSharp.asp)
and the potential problem I see is "Handling Parameterized Properties
with Excel". But that doesn't seem applicable or the first version
wouldn't work correctly. What gives?
 
A

Apurva Sinha \(MS\)

Russell Smith said:
I checked references and worked around the problem but am not sure whether
this is the right approach. The default Shared Add-in project contains a
reference named "Office".

Description: <blank>
Identity: Office
Path: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Office.dll

I noticed that the default VSTO Excel Workbook project contains a
reference named "Microsoft.Office.Core".

Description: Microsoft Office 11.0 Object Library
Identity: {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}\2.3\0\primary
Path:
C:\WINDOWS\assembly\GAC\Office\11.0.0.0__71e9bce111e9429c\Office.dll

When I deleted the Add-In project's Office reference and replaced it with
the same reference used by the Excel Workbook project, it worked fine. I
have no idea which is the correct reference to use. Based on the DLL file
timestamps, the first was installed as part of Office 2003, the second was
installed as part of VSTO.

Please enlighten me.

V/R
Russell

Nicholas Paldino said:
Russel,

The only thing I can think of is that the version of excel that you
are testing your add in against is different than the version that the
interop library was generated for. Is this possible?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Russell Smith said:
I am creating an Excel add-in. Please note project type is Shared Add-in
vice Excel Workbook. In the OnStartupComplete event handler I am adding
a new menu item. I start by getting a reference to the Tools menu. This
works fine.

Type applicationType = applicationObject.GetType();
CommandBars bars = (CommandBars)applicationType.InvokeMember(
"CommandBars", BindingFlags.GetProperty, null, applicationObject,
null);
CommandBar bar = bars["Tools"];
Debug.WriteLine(bar.Name);

But the following more readable code throws "An unhandled exception of
type 'System.ExecutionEngineException' occurred in testaddin.dll" on the
third line.

Excel.Application app = (Excel.Application)applicationObject;
CommandBars bars = app.CommandBars;
CommandBar bar = bars["Tools"];
Debug.WriteLine(bar.Name);

I have read Programming Office Applications Using Microsoft Visual C#
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/OffCSharp.asp)
and the potential problem I see is "Handling Parameterized Properties
with Excel". But that doesn't seem applicable or the first version
wouldn't work correctly. What gives?

Hey Russel,
The second reference is the right one. You can find it under the COM Tab of
the 'Add References' dialog in VS.
Description: Microsoft Office 11.0 Object Library
Identity: {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}\2.3\0\primary
Path:
C:\WINDOWS\assembly\GAC\Office\11.0.0.0__71e9bce111e9429c\Office.dll

As you said, this is the reference added to your project when you create a
VSTO project.

Hope this helps.
Apurva Sinha (MS)
 

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