473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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/de.../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?
Nov 16 '05 #1
3 8380
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]
- mv*@spam.guard.caspershouse.com

"Russell Smith" <rlsmith @ caci dot com> wrote in message
news:Oo***************@TK2MSFTNGP14.phx.gbl...
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/de.../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?

Nov 16 '05 #2
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\Offic e.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__71e9bce11 1e9429c\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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2*****************@TK2MSFTNGP11.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Russell Smith" <rlsmith @ caci dot com> wrote in message
news:Oo***************@TK2MSFTNGP14.phx.gbl...
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/de.../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?

Nov 16 '05 #3

"Russell Smith" <rlsmith @ caci dot com> wrote in message
news:OV**************@TK2MSFTNGP11.phx.gbl...
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\Offic e.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__71e9bce11 1e9429c\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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2*****************@TK2MSFTNGP11.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Russell Smith" <rlsmith @ caci dot com> wrote in message
news:Oo***************@TK2MSFTNGP14.phx.gbl...
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/de.../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__71e9bce11 1e9429c\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)
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: It's me | last post by:
I followed the example in http://stompstompstomp.com/weblog/technical/2004-05-20 and learned that to add a new worksheet to an Excel workbook, you would use the workbook.Worksheets.Add() method. ...
1
by: Bill S | last post by:
Has anyone created HPageBreaks or VPageBreaks from their Application for Excel. I use Excel as my reporting tool for all of my Applications, and have been trying to add a new or move an existing...
3
by: Mike | last post by:
Hello, I am attempting to export the contents of a datagrid on my webform (using Excel automation) through ASP.NET. I have added the Excel object library to my VS ASP.NET project but I am...
3
by: kscheu via AccessMonster.com | last post by:
I am using OutputTo to export a query to excel, prompt the user to save as and then open the excel file. See below: DoCmd.OutputTo acOutputQuery, "q701ExportFields", acFormatXLS, , True 'Opend...
1
by: Gultekin Komanli | last post by:
Hi all, When I try to add reference to Excel, I select my project's porperties from the Project menu, on the add reference dialog, tab named COM, there are Excel 11.0 and Excel 5.0 Object...
18
by: John Bailo | last post by:
I want to write an Excel file (.xls format) from some database data. I don't want to use Excel.exe because of all the automation and security issues. Does Microsoft document the .xls file...
2
by: jesusdiehard | last post by:
hi guys, this is first posting on The Scripts network. Guys, i have built a shared addin for Excel 2003 using visual studio 2005 and C#. Till adding command bar and menu items, it was okay. ...
0
by: madhatter84gn | last post by:
I am trying to automate an excel spreadsheet and then create a chart. Dim chartRange As Excel.Range If rollup Then chartRange = sheet.Range("O2:Q14", Type.Missing) ...
2
by: zer0gg | last post by:
Hi, does anybody know how to set a title to a chart created from vb here's my code .... Dim graficos As Excel.ChartObjects = CType(hoja1.ChartObjects(), Excel.ChartObjects) ...
1
by: =?Utf-8?B?dHRocm9uZQ==?= | last post by:
Hi, I am querying sql server and loading the resulting data into Excel. For character and integer data types, it's working well, but when I pull decimal data and attempt to load it into Excel...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.