• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 924
  • Last Modified:

C# Excel Addin

Hi

I'm in the process of writing a COM add-in for Excel 2003 using C#, most of which is going ok. However, I have a question that I would appreciate some help with....

I need to access the ActiveWorkbook/ActiveWorksheet when Excel opens. I thought I would be able to do this in the OnStartupComplete method. Unfortunately, the Excel.Workbooks.Count property is at zero during the OnStartupComplete method, indicating that the Activeworkbook objects of the file I am opening has not been set yet.

The add-in needs to access the activeworkbook when it opens. Can anyone suggest how this is possible? I am using the method as indicated in the following link: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q302901

Thanks

Jeremy

0
JezzaKashel
Asked:
JezzaKashel
  • 6
  • 5
1 Solution
 
TheAvengerCommented:
Go to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaxl11/html/xlobjApplication1_HV05199211.asp
In the events section you will see different events, e.g. NewWorkbook event. Try which one can help you. The fact that the reference is for VBA should not bother you, you have absolutely the same methods in C#. If none of the application object events works for you, try to search for other object on the left like Workbook and see if this gives you any other option.
0
 
JezzaKashelAuthor Commented:
Thanks...

It seems like I need a workbook open event handler.

I have tried this with the following code:

//Add workbook open event handler
Decs.objExcel.WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(WorkbookOpen);

public void WorkbookOpen(Excel.Workbook wb)
{
}

But I get the error 'No such interface supported' at the point when I use the +=

Has anyone got any ideas how to get the event working?
0
 
TheAvengerCommented:
What version of Excel do you have? I think this reference is for 2003 and if you have an older version, it is possible that some methods don't work
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JezzaKashelAuthor Commented:
It should be ok, I'm using Excel 2003.
0
 
TheAvengerCommented:
But I think you can get the correct interop assemblies from MS as a download
0
 
JezzaKashelAuthor Commented:
Can you give a little more info? I have no idea how that will help me - or how to do it/implement it?

Why would the interop assemblies be incorrect?
0
 
TheAvengerCommented:
The interop assemblies are wrappers around COM classes. The companies that made the COM classes, in this case MS, provide precompiled, correct and signed assemblies. If you don't use them, the IDE will generate automatically these assemblies, but it is highly recommended to use the standard ones.

Can you give me more details about how you created your project? Are you using MS Office Tools for VS .Net?
0
 
JezzaKashelAuthor Commented:
Hi

I pretty much followed the contents of the article at the link below, but I filtered out all the bits to do with Outlook/Access

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q302901

So I took the steps New Project->Extensibilty Projects->Shared Add-in.

Everything woks good using this method, I can access the Excel workbook via menus that I create. I just can't seem to pick up the Workbook Open method.
0
 
TheAvengerCommented:
Here you can get and read about the primary interop assemblies for office 2003:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dno2k3ta/html/OfficePrimaryInteropAssembliesFAQ.asp
I think using them will solve your current problem and will keep you alway from such problems in the future.

Additionally you can read about the VS Tools for Office here:
http://msdn.microsoft.com/library/en-us/odc_2003_ta/html/odc_ancVSTO.asp
I think you need an MSDN subscription to get them however :-(( They will be part of VS 2005.

You can read these pages to find out more about the office objects. E.g. here you can read about Excel objects: http://msdn.microsoft.com/library/en-us/odc_vsto2003_ta/html/ExcelObj.asp
0
 
JezzaKashelAuthor Commented:
Thanks very much for your answer - installing the new interops fixed it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now