Marshal.GetActiveObject COM运行时异常HRESULT 0x800401E3

时间:2017-11-07 12:51:13

标签: c#

我正试图抓住打开的excel应用程序。我正在使用Marshal.GetActiveObject("Excel.Application")方法来执行此操作。我手动打开excel工作簿,然后试着抓住它。只有一个excel实例打开,所以我不确定为什么我会COMExceptionOperation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))

我的代码如下

        Microsoft.Office.Interop.Excel.Application oExcelApp=null;
        oExcelApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") as Excel.Application;

1 个答案:

答案 0 :(得分:0)

克雷格

我用:

    Microsoft.Office.Interop.Excel.Application oExcelApp = null;
    try
    {
       oExcelapp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
    }
    catch {} // can do intelligent stuff here    
    if (oExcelApp == null)
    { Console.WriteLine("Excel not found"); }
    else
    {Console.WriteLine(oExcelApp.ActiveWorkbook.FullName);}