我正在尝试确定是否有一个实例的Excel正在运行并打开了特定的文件,如果是,则附加到它以便我可以控制该实例。
我已经搜索了一些内容,大部分来自this问题。它引用了另一个site,但不幸的是对我来说已经失效了,所以我无法阅读它。
到目前为止,我的代码如下:
//Is Excel open?
if (Process.GetProcessesByName("EXCEL").Length != 0)
{
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
//Find the exact Excel instance
if (process.ProcessName.ToString() == "EXCEL" && process.MainWindowTitle == ("Microsoft Excel - " + fileName))
{
//Get the process ID of that instance
int processID = (int)Process.GetProcessById(process.Id).MainWindowHandle;
//Attach to the instance...
Excel.Application existingExcel = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject(process.Id);
}
}
}
到目前为止,我已经成功获取了我想要附加的实例的进程ID,但是在使用该ID时我感到困惑。
有什么建议吗?