J
JP Jones
I had the same problem until I found an example that I tailored to
terminate all instances of Escel. Here's the code.
Sub TerminateProcess()
' Terminate the Excel Process
Dim strTerminateThis As String 'The variable to hold the process
to terminate
Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
Dim intError As Integer
strTerminateThis = "Excel.exe" 'Process to terminate,
Set objWMIcimv2 = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2") 'Connect to
CIMV2 Namespace
Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='" & strTerminateThis &
"'") 'Find the process to terminate
If objList.Count = 0 Then 'If 0 then process isn't running
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
Exit Sub
Else
For Each objProcess In objList
intError = objProcess.Terminate 'Terminates a process and
all of its threads.
'Return value is 0 for success. Any other number is an
error.
If intError <> 0 Then
MsgBox "ERROR: Unable to terminate that process.",
vbCritical, "Aborting"
Exit Sub
End If
Next
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
Exit Sub
End If
End Sub
terminate all instances of Escel. Here's the code.
Sub TerminateProcess()
' Terminate the Excel Process
Dim strTerminateThis As String 'The variable to hold the process
to terminate
Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
Dim intError As Integer
strTerminateThis = "Excel.exe" 'Process to terminate,
Set objWMIcimv2 = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2") 'Connect to
CIMV2 Namespace
Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='" & strTerminateThis &
"'") 'Find the process to terminate
If objList.Count = 0 Then 'If 0 then process isn't running
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
Exit Sub
Else
For Each objProcess In objList
intError = objProcess.Terminate 'Terminates a process and
all of its threads.
'Return value is 0 for success. Any other number is an
error.
If intError <> 0 Then
MsgBox "ERROR: Unable to terminate that process.",
vbCritical, "Aborting"
Exit Sub
End If
Next
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
Exit Sub
End If
End Sub