我如何能杀死通过VBA代码,任务管理器进程?我如何能杀死通过VBA代码,任务管理器进程?(How c

2019-05-12 10:02发布

我试图通过VBA来中止特定进程。 我有一个连接到市场数据总线的专有对象。 通过RTD我把这个对象的pub / sub到总线。 然而有时连接中断,我需要通过击杀任务管理器的进程。 有没有一种方法杀死通过VBA的过程?

谢谢

Answer 1:

与此代码尝试

Dim oServ As Object
Dim cProc As Variant
Dim oProc As Object

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

For Each oProc In cProc

    'Rename EXCEL.EXE in the line below with the process that you need to Terminate. 
    'NOTE: It is 'case sensitive

    If oProc.Name = "EXCEL.EXE" Then
      MsgBox "KILL"   ' used to display a message for testing pur
      oProc.Terminate()
    End If
Next


Answer 2:

看看更多的一个例子:

Sub Test()
    If TaskKill("notepad.exe") = 0 Then MsgBox "Terminated" Else MsgBox "Failed"
End Sub

Function TaskKill(sTaskName)
    TaskKill = CreateObject("WScript.Shell").Run("taskkill /f /im " & sTaskName, 0, True)
End Function


Answer 3:

您可以执行它,这种方式:

Dim oServ As Object
Dim cProc As Variant
Dim oProc As Object

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

For Each oProc In cProc

    'Rename EXCEL.EXE in the line below with the process that you need to Terminate. 
    'NOTE: It is 'case sensitive

    If oProc.Name = "EXCEL.EXE" Then
      MsgBox "KILL"   ' used to display a message for testing pur
      oProc.Terminate  'kill exe
    End If
Next


Answer 4:

Sub Kill_Excel()

Dim sKillExcel As String

sKillExcel = "TASKKILL /F /IM Excel.exe"
Shell sKillExcel, vbHide

End Sub


文章来源: How can I kill task manager processes through VBA code?