1

Trying to kill InternetExplorer:

Sub IE_kill()

    Dim objWMI As Object, objProcess As Object, objProcesses As Object
    Set objWMI = GetObject("winmgmts://.")
    Set objProcesses = objWMI.ExecQuery( _
        "SELECT * FROM Win32_Process WHERE Name = 'iexplore.exe'")

    For Each objProcess In objProcesses
        If Not objProcess Is Nothing Then
        hh = objProcesses.Count ' 1
            objProcess.Terminate ' Here is Error Not Found
            If Err.Number <> 0 Then
            Else
                'DisplayErrorInfo
                Exit For
            End If
        End If
    Next
    Set objProcesses = Nothing: Set objWMI = Nothing


End Sub

but get sometimes error on objProcess.Terminate Not Found how to solve problem? Error catch do not help. On error resume next not work as error raise instead.

Community
  • 1
  • 1
Dmitrij Holkin
  • 1,995
  • 3
  • 39
  • 86

2 Answers2

2

I have tried this modification of your code (for MS Edge) and it worked about 3 times so far:

Option Explicit

Sub KillIE()

    Dim objWMI As Object, objProcess As Object, objProcesses As Object        
    Set objWMI = GetObject("winmgmts://.")        
    Set objProcesses = objWMI.ExecQuery("SELECT * FROM Win32_Process")

    For Each objProcess In objProcesses
        If Not objProcess Is Nothing Then
            If InStr(1, UCase(objProcess.Name), "EDGE") > 0 Then                    
                Debug.Print objProcess.Name
                objProcess.Terminate                    
                If Not Err.Number <> 0 Then
                    Exit For
                End If                    
            End If
        End If
    Next
End Sub

You can give it a try and check the objProcess.Name, before it gives an error. Consider replacing "EDGE" with INTERNETEXPLORER or IEXPLORER.

Vityata
  • 42,633
  • 8
  • 55
  • 100
1

As mentioned in one of the comments, you can use the taskkill command as shown below:

Sub IE_kill
    Dim objShell, strCommand
    strCommand = "taskkill /f /im iexplore.exe"
    Set objShell = CreateObject("wscript.shell")
    objShell.Run strCommand
    Set objShell = Nothing
End Sub

Check this answer out to know more about the taskkill

OR, if you want to stick to the wmi, you can try the following "workaround"(it will not throw the error you are currently getting-see further explanation in comments):

Dim objw, strComputer, arrPID(), intIndex
strComputer = "."
intIndex=-1
Set objw = GetObject("winmgmts://"&strComputer&"/root/cimv2")
Set objps = objw.ExecQuery("Select * from win32_process where name = 'iexplore.exe'")

for each instance in objps
    intIndex = intIndex + 1
    redim preserve arrPID(intIndex)
    arrPID(intIndex) = instance.processID        'storing all the process IDs of the processes having name = iexplore.exe
next

for each pid in arrPID
    Set objps = objw.ExecQuery("Select * from win32_process where processID = "&pid)     'getting the process with the process IDs
    for each instance in objps
        instance.terminate           'As soon as this line gets executed for the first process ID in the array, It will terminate ALL the iexplore processes. This means, for the remaining process IDs in the array, this line would not even get executed because when we try to find the process with that process ID, it wouldn't be found and hence we would not be able to enter the for-loop and tus no error is generated.
    next
next
Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43