I need the Excel VBA equivalent code for "If Process exists then....endif".
By "process" do you mean a Windows process? Or something else? I
think you
need to clarify exactly what you want to test for.
For a Windows process whose Process ID is known, use code similar to
the
following:
''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function GetProcessVersion Lib "kernel32" ( _
ByVal ProcessID As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Sub AAA()
Dim ProcID As Long
Dim Version As Long
Dim LastErr As Long
Dim ThisProcID As Long
ThisProcID = GetCurrentProcessId()
ProcID = 1234& ' specify process ID
' OR
ProcID = ThisProcID ' this process ID
Version = GetProcessVersion(ProcID)
LastErr = Err.LastDllError
If Version = 0 Then
If LastErr = &H57 Then
' processs does not exist
Debug.Print "Process does not exist"
Else
' process exists but access is denied
Debug.Print "Err: " & CStr(LastErr), _
GetSystemErrorMessageText(LastErr)
End If
Else
' process exists and is accessible:
Debug.Print "Process Exists. Version: " & Hex(Version)
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)