how to kill a app from task manager with VBA?

I am looking for a way to to kill a running exe from task manager.
MikeYoungMoonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Professor JMicrosoft Excel ExpertCommented:
Lets say you want to Kill Notepad.

here it is

Sub TestKill()


Dim strTerminateThis As String
Dim objWMIcimv2 As Object, objProcess As Object, objList As Object
Dim intError As Integer


strTerminateThis = "Notepad.exe"


Set objWMIcimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objList = objWMIcimv2.ExecQuery("select * from win32_process where name='" & strTerminateThis & "'")
For Each objProcess In objList
            intError = objProcess.Terminate

            If intError <> 0 Then Exit For
Next


Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing

End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieAnalyst Assistant Commented:
How do you want to specify the exe to kill?

This will ask you to enter the name of the exe via an input box.
Sub KillEXE()
Dim oServ As Object
Dim oProc As Object
Dim cProc As Variant
Dim strEXEName As String
Dim Res As VbMsgBoxResult

    Set oServ = GetObject("winmgmts:")
    Set cProc = oServ.ExecQuery("Select * from Win32_Process")

    strEXEName = InputBox("Please enter name of process to kill:", "Kill process")
    
    For Each oProc In cProc
        
        If oProc.Name = "EXCEL.EXE" Then
        
            Res = MsgBox("Are you sure you want to kill this process?" & vbCrLf & vbCrLf & vbTab & vbTab & strEXEName, vbYesNo + vbQuestion, "Kill " & strEXEName)
            
            If Res = vbYes Then
                oProc.Terminate
            Else
                Exit Sub
            End If
            
        End If
        
    Next oProc
    
End Sub

Open in new window

MikeYoungMoonAuthor Commented:
Thank you both.

Both codes works.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.