gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:42:54 25 4
gpt4 key购买 nike

我继承了一些现在不起作用的代码。它应该使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com