试图杀死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
但有时得到错误的objProcess.Terminate
却找不到解决问题的办法?错误捕获没有帮助。在错误恢复下一步,而不是作为错误引发工作。
发布于 2018-04-09 14:20:59
如其中一个注释所述,您可以使用taskkill
命令,如下所示:
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
查看this答案,以了解更多关于taskkill
的信息
或者,如果您想坚持使用wmi
,可以尝试以下“解决方法”(它不会抛出当前正在收到的错误--请参阅注释中的进一步解释):
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
发布于 2018-04-09 11:23:50
我已经尝试过对您的代码进行这种修改(针对MS ),到目前为止,它已经成功了3次:
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
您可以尝试并检查objProcess.Name
,然后才会出现错误。考虑将"EDGE"
替换为INTERNETEXPLORER
或IEXPLORER
。
https://stackoverflow.com/questions/49731486
复制相似问题