首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >objProcess.Terminate未找到

objProcess.Terminate未找到
EN

Stack Overflow用户
提问于 2018-04-09 11:06:16
回答 2查看 1K关注 0票数 1

试图杀死InternetExplorer:

代码语言:javascript
运行
复制
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却找不到解决问题的办法?错误捕获没有帮助。在错误恢复下一步,而不是作为错误引发工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-09 14:20:59

如其中一个注释所述,您可以使用taskkill命令,如下所示:

代码语言:javascript
运行
复制
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,可以尝试以下“解决方法”(它不会抛出当前正在收到的错误--请参阅注释中的进一步解释):

代码语言:javascript
运行
复制
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
票数 1
EN

Stack Overflow用户

发布于 2018-04-09 11:23:50

我已经尝试过对您的代码进行这种修改(针对MS ),到目前为止,它已经成功了3次:

代码语言:javascript
运行
复制
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"替换为INTERNETEXPLORERIEXPLORER

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49731486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档