Public Function StopMyProcess(strTerminateThis As String)
On Error Resume Next
Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
Dim intError As Integer
Set objWMIcimv2 = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='" & strTerminateThis & "'")
If objList.Count = 0 Then
'The process isn't running
MsgBox "The process isn't running."
Else
'The process is running
For Each objProcess In objList
'Terminate the process and all of its threads.
intError = objProcess.Terminate
If intError <> 0 Then
'Return value is 0 for success. Any other number is an error.
MsgBox "There was an error closing this process."
Else
MsgBox "Process terminated ok."
End If
Next
Set objProcess = Nothing
End If
Set objWMIcimv2 = Nothing
Set objList = Nothing
End Function