473,750 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 OnStartupComple te event handler I am adding a
new menu item. I start by getting a reference to the Tools menu. This
works fine.

Type applicationType = applicationObje ct.GetType();
CommandBars bars = (CommandBars)ap plicationType.I nvokeMember(
"CommandBar s", BindingFlags.Ge tProperty, null, applicationObje ct,
null);
CommandBar bar = bars["Tools"];
Debug.WriteLine (bar.Name);

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

Excel.Applicati on app = (Excel.Applicat ion)application Object;
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 8409
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.co m

"Russell Smith" <rlsmith @ caci dot com> wrote in message
news:Oo******** *******@TK2MSFT NGP14.phx.gbl.. .
I am creating an Excel add-in. Please note project type is Shared Add-in
vice Excel Workbook. In the OnStartupComple te event handler I am adding a
new menu item. I start by getting a reference to the Tools menu. This
works fine.

Type applicationType = applicationObje ct.GetType();
CommandBars bars = (CommandBars)ap plicationType.I nvokeMember(
"CommandBar s", BindingFlags.Ge tProperty, null, applicationObje ct,
null);
CommandBar bar = bars["Tools"];
Debug.WriteLine (bar.Name);

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

Excel.Applicati on app = (Excel.Applicat ion)application Object;
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\Micr osoft.NET\Frame work\v1.1.4322\ Office.dll

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

Description: Microsoft Office 11.0 Object Library
Identity: {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}\2 .3\0\primary
Path:
C:\WINDOWS\asse mbly\GAC\Office \11.0.0.0__71e9 bce111e9429c\Of fice.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.c om> wrote in
message news:%2******** *********@TK2MS FTNGP11.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.co m

"Russell Smith" <rlsmith @ caci dot com> wrote in message
news:Oo******** *******@TK2MSFT NGP14.phx.gbl.. .
I am creating an Excel add-in. Please note project type is Shared Add-in
vice Excel Workbook. In the OnStartupComple te event handler I am adding a
new menu item. I start by getting a reference to the Tools menu. This
works fine.

Type applicationType = applicationObje ct.GetType();
CommandBars bars = (CommandBars)ap plicationType.I nvokeMember(
"CommandBar s", BindingFlags.Ge tProperty, null, applicationObje ct,
null);
CommandBar bar = bars["Tools"];
Debug.WriteLine (bar.Name);

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

Excel.Applicati on app = (Excel.Applicat ion)application Object;
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******** ******@TK2MSFTN GP11.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\Micr osoft.NET\Frame work\v1.1.4322\ Office.dll

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

Description: Microsoft Office 11.0 Object Library
Identity: {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}\2 .3\0\primary
Path:
C:\WINDOWS\asse mbly\GAC\Office \11.0.0.0__71e9 bce111e9429c\Of fice.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.c om> wrote
in message news:%2******** *********@TK2MS FTNGP11.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.co m

"Russell Smith" <rlsmith @ caci dot com> wrote in message
news:Oo******** *******@TK2MSFT NGP14.phx.gbl.. .
I am creating an Excel add-in. Please note project type is Shared Add-in
vice Excel Workbook. In the OnStartupComple te event handler I am adding
a new menu item. I start by getting a reference to the Tools menu. This
works fine.

Type applicationType = applicationObje ct.GetType();
CommandBars bars = (CommandBars)ap plicationType.I nvokeMember(
"CommandBar s", BindingFlags.Ge tProperty, null, applicationObje ct,
null);
CommandBar bar = bars["Tools"];
Debug.WriteLine (bar.Name);

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

Excel.Applicati on app = (Excel.Applicat ion)application Object;
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\asse mbly\GAC\Office \11.0.0.0__71e9 bce111e9429c\Of fice.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
1887
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. That works. However, the new worksheet got added *in front* of the last worksheet I was at. How can I get it to add *after*? Thanks, --
1
2792
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 pagebreak. But can not get code to work correctly. I have pasted some code that works up to the page break. If someone could help me or post back some code that works. Thanks oWB = oXL.Workbooks.Add oSheet = oWB.ActiveSheet With oSheet...
3
1837
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 lost on how to export my datagrid contents to Excel? I am sure it can be done but having difficulties figuring out the code.
3
3113
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 Excel Automatically Now I want to add a row to the opened spreadsheet at the top and add a title. How do I accomplish this? I am new to excel formatting, but I created a macro and the code to do this is: Rows("1:1").Select Selection.Insert...
1
2035
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 Libraries. If I select Excel 11.0 object library, after a few seconds reference shows itself on the references section of my project's properties window with and error on the path column : "the system cannot find the reference specified".
18
25728
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 format anywhere? Is there any c# class that will let me do this?
2
3596
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. But now i want to add typical button on excel sheet programmatically, but nothing seems to working. Is it possible that i can add typical button controls on excel sheet from program.
0
2519
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) chartRange.Select() Else chartRange = sheet.Range("N:N,M:M,A:A", Type.Missing) chartRange.Select() End If
2
1299
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) Dim objgrafico As Excel.ChartObject = graficos.Add(100, 50, 500, 300) Dim grafico As Excel.Chart = objgrafico.Chart grafico.ChartType = Excel.XlChartType.xl3DPie
1
2229
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 money format it fails. Below is a sample of the code I'm running... Dim strRangeString As String Dim aRange As Excel.Range Dim decArray As Array dim i as Integer
0
8836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9394
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.