Detecting existing Excel instance the .Net way?

  • Thread starter Thread starter Fred Morrison
  • Start date Start date
F

Fred Morrison

In VB6, I would check to see if Excel was already running by this technique

Private m_booExcelCreatedHere As Boolean

Dim xlApp as Excel.Application

On Error Resume Next ' temporarily suppress error handler
Set xlApp = GetObject(,"Excel.Application")
On Error GoTo PROC_ERR ' resume normal error handling
If xlApp Is Nothing Then
Set xlApp = New Excel.Application
' xlApp.Visible = True ' uncomment when debugging to see what's
happening
m_booExcelCreatedHere = True
Else
m_booExcelCreatedHere = False
End If

Later on, I'd use m_booExcelCreatedHere to determine if I should leave Excel
running (perhaps because the user had started it themselves and is working
with other workbooks unrelated to mine) or use xlApp.Quit and set xlApp to
Nothing.

What's the VB.Net way of doing this?
 
Hi,

If Process.GetProcessesByName("excel").GetLength(0) > 0 Then

MessageBox.Show("Excel is running")

Else

MessageBox.Show("Not running")

End If

Ken
 
Back
Top