vba检查特定进程任务ID是否仍在运行

时间:2010-10-20 18:06:41

标签: ms-access vba shell

我正在使用Shell()函数在MS Access中执行外部应用程序。 Shell()返回已启动的特定进程的任务ID。

我需要一种方法来检查该进程是否仍在运行或是否已关闭。我确信我需要做一些Windows API调用,但我现在似乎找不到任何东西。

3 个答案:

答案 0 :(得分:3)

这是一个Access常见问题解答,多年前在常见问题解答网站上为comp.databases.ms-access,http://mvps.org/access/提供了答案。

API: Shell and Wait

答案 1 :(得分:2)

Shell Tasklisthttp://technet.microsoft.com/en-us/library/bb491010.aspx处的说明)是手动执行此操作的好方法。不过,我不确定如何通过自动化与PID列表进行交互。这有点困难。

提示:如果您只想终止进程,请使用Shell "TaskKill /F /IM ""excel.exe"""来终止所有打开的MS Excel实例。或Shell "TaskKill /F /IM ""msaccess.exe"""杀死所有打开的MS Access实例(包括托管您的VBA脚本的Access文件),不幸的是。这就是我结束标准问题Error Handler的方法。

修改

当你运行它时,你应该这样: alt text

答案 2 :(得分:0)

我使用以下内容作为WMI的一部分:

Public Function WaitForProcess(ProcessName As String, Optional ProcessID As Boolean = False)

    ' if you dont mind including the reference for Microsoft WMI Scripting V.1.2 Library, uncomment below
    ' Otherwise, it should work fine without the reference, just no intelasense

    UpdateStatus "Starting WaitForProcess function to wait for process: " & ProcessName

    Dim strComputer As String
    Dim colProcesses
    Dim objWMIService

    UpdateStatus "Use this computer to see processes on"
    strComputer = "."

    UpdateStatus "Impersonate this computer"
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    UpdateStatus "Get all running processes with a " & IIf(ProcessID = False, "Name", "ProcessID") & " of: " & ProcessName
    Set colProcesses = objWMIService.ExecQuery("SELECT * " & _
                                               "FROM Win32_Process  " & _
                                               "WHERE " & IIf(ProcessID = False, "Name", "ProcessID") & " = '" & ProcessName & "'")

    UpdateStatus "Waiting untill there are no more instances of this process shown in the process list"
    Do
        DoEvents
        ' Update the collection of processes returned

        Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE " & IIf(ProcessID = False, "Name", "ProcessID") & " = '" & ProcessName & "'")


    Loop Until colProcesses.Count = 0

    UpdateStatus "The " & IIf(ProcessID = False, "Process Name", "Process ID") & ": " & ProcessName & " concluded successfully. WaitForProcess Completed"

End Function

这适用于我的项目;但是,它确实需要Windows Management Instrumentation。