I have this code in a C # add-in for VBE (highlighting "VBE": not an MS-Office add-in):
public abstract class HostApplicationBase<TApplication> : IHostApplication
where TApplication : class
{
protected readonly TApplication Application;
protected HostApplicationBase(string applicationName)
{
Application = (TApplication)Marshal.GetActiveObject(applicationName + ".Application");
}
Where TApplication
- interop class Application
MS-Office, for example, type Microsoft.Office.Interop.Excel.Application
; here the parameter applicationName
will be "Excel" for Excel.
The problem is that Marshal.GetActiveObject
only the first created instance seems to return, and this is not necessarily the instance that hosts the current VBE environment, and this causes problems .
How can I get a real host application instance?
source
share