I try to get Excel application in my code with this method:
Excel.Application xlApp = GetApplication();
if (xlApp == null) xlApp = new Excel.Application();
where
private Excel.Application GetApplication()
{
Excel.Application result = null;
try
{
result = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
}
catch (System.Runtime.InteropServices.COMException ex)
{
//Excel is not open
}
return result;
}
but the
System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
always throws exception, even when Excel application is open.
Exception: HRESULT: 0x800401E3
StackTrace:
in System.Runtime.InteropServices.Marshal.GetActiveObject(Guid& rclsid, IntPtr reserved, Object& ppunk)
in System.Runtime.InteropServices.Marshal.GetActiveObject(String progID)
in RaceToolTests.UnitTest1.GetApplication() in C:\Users\...
Please try following code: