我编写了以下过程来检查 Microsoft Excel 应用程序(如果已打开)。该过程工作正常,除了一旦打开工作簿并激活工作表,Excel 的第二个实例就会尝试打开。
这是我的代码:
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlWBName As String = "2011.1004.Compensation Template"
For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
If p.ProcessName <> "EXCEL" Then
xlApp.Visible = True
xlBook = xlApp.Workbooks.Open("F:\Test Environment\Compensation Workbook\Compensation Workbook\bin\Debug\" & xlWBName & ".xlsx")
Dim xlSheet As Excel.Worksheet
xlSheet = CType(xlBook.Sheets("SummaryWorksheet"), Worksheet)
xlSheet.Activate()
End If
Next