Terminating Excel

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
 
M

MJ

JP,

I think your posting would be helpful to more people if it were in the
correct discussion group.

This discussion group is focused on Access macros, not Excel macros.

Good luck,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top