Process.Start(“ Excel.exe”)立即退出

时间:2019-12-26 21:23:04

标签: c# system.diagnostics process.start

我继承了一些现在不起作用的代码。应该使用Process类来启动“ Excel.exe”。但是,如果我在下一行中断,则可以看到我的流程实例有错误:“ process.BasePriority”引发了“ System.InvalidOperationException”类型的异常。下一行代码将引发以下异常:

  

System.InvalidOperationException:无法处理请求,因为该进程已退出。

代码如下:

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的情况下该过程令人兴奋。

过去一年一直运行良好,几天前才停止工作。我对Process类没有任何经验,并且不确定错误可能是什么原因,因为错误似乎非常模糊。基于其他SO线程,看来它可能正在退出。我不知道为什么该过程立即退出。

为澄清起见而编辑。

1 个答案:

答案 0 :(得分:2)

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

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

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

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

也许这就是所有东西的原因吗?