How to call VSTO AddIn method from a separate C # project?

I have a C # Excel add-in project MyExcelAddIn that has the public Foo () method to do something complicated. For testing purposes, the add-in also defines a toolbar button that is connected to Foo (), so I can check this and make sure that clicking the button calls Foo () and does what I want to do. This is normal.

Now I want to call this method from a C # Windows Forms project. In a Windows Forms project, I can create an Excel instance and make it visible and check if my VSTO add-in works, since I can see the button and it works. But I canโ€™t decide how to call Foo () programmatically from a Windows Forms project. I searched a little Google and reached the COMAddIn object "MyExcelAddIn", but I canโ€™t decide how to call Foo ().

It looks something like this:

// Create Excel and make it visible Application excelApp = new Application(); excelApp.Visible = true; // I know my VSTO add-in is running because I can see my test button // Now get a reference to my VSTO add-in Microsoft.Office.Core.COMAddIns comAddIns = _excelApp.COMAddIns; object addinName = "MyExcelAddIn"; Microsoft.Office.Core.COMAddIn myAddin = comAddIns.Item(ref addinName); // This works, but now what? How do I make a call on myAddin? // Note that myAddin.Object is null... 

So, I want to know what I can do to call Foo () from my Windows Forms application. Note that I have full control over the Windows Forms application and add-in, and I suspect that I have to make changes to both of them (especially the add-in), but I have no idea how to do this.

Please note that this is VS2008 C # application and I am using Excel 2003.

+4
source share
4 answers

I am using the SendMessage Win32 API for this. My C # add-on creates a "NativeWindow" with the name of the uniqe window that the WinForm application can find.

+1
source

If you are creating an application-level add-in, I believe this might be your answer: MSST VSTO Article

It includes two steps: (From the article)

  • Expose the object to other solutions in your add-in.
  • In another solution, access the object opened by your add-in and call the members of the object.

Another solution could be: (again from the article)

  • Any solution that runs in a process other than your add-in (these types of solutions are also called clients outside the process). These include applications that automate an Office application, such as Windows Forms or a console application, and add-ins that load into another process.
+4
source

I am assuming your Foo method is somehow interacting with Excel. Otherwise, you can simply add a reference to the assembly containing the class using the Foo method and call it there without creating an instance of Excel.

The only other way I can think of is to get a reference to your CommandBarButton through an excelApp object. CommandBarButton has a method called Execute, similar to a button click. Something like that:

  Excel.Application excelApp = new Excel.Application(); CommandBarButton btn = excelApp.CommandBars.FindControl(...) as CommandBarButton; btn.Execute(); 
0
source

For everyone who finds this here, what I did:

  object addInName = "AddinName"; var excelApplication = (Microsoft.Office.Interop.Excel.Application)Marshal.GetActiveObject("Excel.Application"); COMAddIn addIn = excelApplication.COMAddIns.Item(ref addInName); addIn.Object.AddinMethodName(params); 

It was also necessary to add a link to Microsoft.Office.Core under COM and Excel.Interop under assemblies.

0
source

All Articles