返回列表 上一主題 發帖

[發問] 如何用VBA關閉外部程式

本帖最後由 quickfixer 於 2018-8-17 01:50 編輯

試看看


Sub stopApp()
   
    Dim app As String
    Dim objWMI As Object, objProcess As Object, objList As Object
    Dim intError As Integer



    app = "calc.exe" '這裡填入要關閉的執行檔名稱


    Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set objList = objWMI.ExecQuery("select * from win32_process where name='" & app & "'")
   
    For Each objProcess In objList
        intError = objProcess.Terminate
        If intError <> 0 Then Exit For
    Next

    Set objWMI = Nothing
    Set objList = Nothing
    Set objProcess = Nothing

End Sub

TOP

        靜思自在 : 對父母要知恩,感恩、報恩。
返回列表 上一主題