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

标签 c# system.diagnostics process.start

我继承了一些现在不起作用的代码。它应该使用 Process 类来启动“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 的情况下兴奋了。

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

已编辑以澄清。

最佳答案

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

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

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

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

也许这就是所有事情的原因?

关于c# - Process.Start ("Excel.exe") 立即退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59493594/

相关文章:

c# - Process.Start() 和手动运行有什么区别?

c# - 将 ProcessStartInfo.WorkingDirectory 设置为 UNC 路径

c# - 如何在 C# Zip 中组合三个数组?

asp.net - System.Diagnostics.Trace 在 ApplicationPoolIdentity 下的 Web 应用程序中不起作用

c# - 有没有办法获取在本地虚拟机上运行的进程列表?

c# - 显示 Internet 选项,例如 Internet Explorer

c# - 当 UAC 被拒绝时,Process.Start 永远不会返回

c# - 为什么 C# 不能自动提供对事件的线程安全访问,而 C++/CLI 可以?

c# - 在 Ubuntu 上使用带有 Mono 的 HttpWebResponse 清空响应数据

c# - Telegram Bot 下载图像文件