C# GetObject Method
Marshal.GetActiveObject gets a running instance of the specified object from the Running Object Table (ROT).
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
private void GetExcel()
{
try
{
oXL = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Marshal.GetActiveObject exposes the GetActiveObject COM API method from OLEAUT32.DLL; however, the latter expects a class identifier (CLSID) instead of the programmatic identifier (ProgID) expected by this method. To obtain a running instance of a COM object without a registered ProgID, use platform invoke to define the GetActivateObject COM method