Process.Start("Excel.exe") 立即退出

Process.Start("Excel.exe") exits immediately

我继承了一些现在不起作用的代码。它应该使用进程 class 来启动 "Excel.exe"。但是如果我在下一行中断,我可以看到我的流程实例有错误:'process.BasePriority' 抛出了类型 'System.InvalidOperationException' 的异常。下一行代码抛出以下异常:

System.InvalidOperationException: Cannot process request because the process has exited.

代码如下:

private Microsoft.Office.Interop.Excel.Application StartExcel()
{
    // Maximum number of attempts to look for started Excel Application
    const int maxAttempts = 3;
    // Number of milliseconds to wait between attempts to look for started Excel Application
    const int waitTimeMS = 200;

    Microsoft.Office.Interop.Excel.Application result = null;

    // Start Excel
    var process = Process.Start("Excel.exe");
    //process.WaitForInputIdle();

    // Try to find started Excel Application

    int currentAttempt = 1;

    while ((result == null) && (currentAttempt <= maxAttempts))
    {
        // Wait between attempts 
        if (currentAttempt != 1)
        {
            Thread.Sleep(waitTimeMS);
        }

        // List all running Excel automation objects and find the one with the same process id
        IRunningObjectTable lRunningObjectTable = null;
        IEnumMoniker lMonikerList = null;

        try
        {
            // Query Running Object Table 
            if (GetRunningObjectTable(0, out lRunningObjectTable) == 0 && lRunningObjectTable != null)
            {

                // List Monikers
                lRunningObjectTable.EnumRunning(out lMonikerList);

                // Start Enumeration
                lMonikerList.Reset();

                // Array used for enumerating Monikers
                IMoniker[] lMonikerContainer = new IMoniker[1];

                IntPtr lPointerFetchedMonikers = IntPtr.Zero;

                // foreach Moniker
                while (lMonikerList.Next(1, lMonikerContainer, lPointerFetchedMonikers) == 0)
                {
                    object lComObject;
                    lRunningObjectTable.GetObject(lMonikerContainer[0], out lComObject);

                    // Check the object is an Excel workbook
                    if (lComObject is Microsoft.Office.Interop.Excel.Workbook)
                    {
                        Microsoft.Office.Interop.Excel.Workbook lExcelWorkbook = (Microsoft.Office.Interop.Excel.Workbook)lComObject;

                        // Get the Process ID for the Window Handle 
                        uint processId;
                        GetWindowThreadProcessId(new IntPtr(lExcelWorkbook.Application.Hwnd), out processId);

                        if (processId == process.Id)
                        {
                            // Correct automation object found, return Application
                            result = lExcelWorkbook.Application;
                            break;
                        }
                    }
                }
            }
        }
        finally
        {
            // Release ressources
            if (lRunningObjectTable != null) Marshal.ReleaseComObject(lRunningObjectTable);
            if (lMonikerList != null) Marshal.ReleaseComObject(lMonikerList);
        }

        currentAttempt++;
    }
    return result;
}

最终结果 = null。此外,excel 工作簿确实出现在我的屏幕上,但由于某种原因,进程无法找到 Excel 工作簿进程 ID。我猜这是因为进程已经兴奋而没有关闭 Excel.exe。

过去一年运行都很好,几天前才停止工作。我对过程 class 没有任何经验,并且不确定问题可能是什么,因为错误看起来非常模糊。基于其他 SO 线程,它似乎正在退出。我不明白为什么这个过程会立即退出。

编辑以澄清。

如果我是你,我会尝试将整个方法替换为:

private Microsoft.Office.Interop.Excel.Application StartExcel()
{
    return new Microsoft.Office.Interop.Excel.Application();
}

我想如果您在同一台机器上安装了多个版本,interop 可能会使用错误的 excel 版本?

我有两个版本,当 Process.Start("excel") 打开较新版本时,我的互操作正在打开 excel 2013 的实例?

也许这就是所有这些的原因?