Communication between two Excel COM add-ins

A

Alex

Hello all,

I need to communicate an Excel COM Add-in and a RTD automation server
that run at the same time when I open Excel. I need the RTD server to
get a reference to the COM add-in.

To do that I do the following:

In the COM Addin:
class MyCOMAddin : Extensibility.IDTExtensibility2
{

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
((COMAddIn)addInInst).Object = this;
}

}


In the RTD:
public int ServerStart(Excel.IRTDUpdateEvent CallbackObject)
{
Excel._Application app = (Excel._Application)
Marshal.GetActiveObject("Excel.Application");

MyCOMAddin addin = (MyCOMAddin) applicationObject.COMAddIns.Item
(ref progName).Object; // progName is "MyCOMAddin.Connect"

}


This approach works but has two problems:

1) Marshal.GetActiveObject("Excel.Application") may fail. So I may
have to retry this operation and try to force that object to be
registered. To do that I have to minimize and maximize the Excel
window when the addin is loaded.

2) If I open a second Excel application (not another workbook, a new
Excel instance). All this will fail. In particular, a new call to
applicationObject.COMAddIns.Item(ref progName).Object will return a
System.__ComObject that is not possible to cast to MyCOMAddin.

I couldn't find anything to solve that, do you have any solution for
these 2 issues?

Thanks!
 

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