我爱帮助网-手册QQ交流群

Nas交流与矿渣群(unraid 群晖 猫盘 蜗牛等):580680114         物联网/智能家居群:518812757             帮助教程:手册大全

软件使用与建站群:1057308983      虚拟化交流群:13448651

0

VBA 使用Shell执行命令并等待它结束返回(三种不同的方法)


方法一:使用WaitForSingleObject 等待命令执行完成结束返回



Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFFFFFF
 
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
 
Dim lngPId As Long
Dim lngPHandle As Long
 
lngPId = Shell("52Help.bat", vbHide)
lngPHandle = OpenProcess(SYNCHRONIZE, 0, lngPId)
If lngPHandle <> 0 Then
    Call WaitForSingleObject(lngPHandle, INFINITE) '无限等待,直到程序命令执行结束返回
    Call CloseHandle(lngPHandle)
End If



方法二:VBA 使用WScript.Shell执行命令并等待它结束

Sub ShellAndWait(pathFile As String)
    With CreateObject("WScript.Shell")
        .Run pathFile, 1, True
    End With
End Sub

 以下是调用 示例


使用示例:

Sub demo_Wait()
    ShellAndWait ("notepad.exe")
    Beep "直到上面执行完成后才会执行后面的命令程序,一直等待上面程序结束 "
    MsgBox "Done!"
End Sub



方法三、如何确定 shell 进程何时结束(微软官方的方法)


当您在 Visual Basic for Applications (VBA) 过程中运行**命令行** 管理程序函数时, 它将异步启动一个可执行程序并将控制返回给该过程。 此 Shelled 程序将继续独立于您的过程运行,直到关闭它。

如果您的过程需要等待 Shelled 进程结束,则可以使用 Windows API 来轮询应用程序的状态,但此方法并不是很有效。 本主题介绍一种更有效的方法。

Windows API 具有集成功能,该功能可以使应用程序等待,直到 Shelled 进程完成。 要使用这些函数,需要有 Shelled 程序的句柄。 要完成此操作,应使用 CreateProcess 函数而不是 Shell 函数来开始 Shelled 程序。

创建外壳过程

若要创建可寻址进程,请使用 CreateProcess 函数来启动 Shelled 应用程序。 CreateProcess 函数会通过它传递的其中一个参数来为您的程序提供 Shelled 进程的进程句柄。

等待外壳过程结束

使用 CreateProcess 函数取得进程句柄后,可以将该句柄传递给 WaitForSingleObject 函数。 这样会使 VBA 过程暂停执行,直到 Shelled 程序结束。

构建使用 CreateProcess 函数运行 Windows 记事本应用程序的 VBA 过程需要执行下列步骤。 以下代码说明在继续执行之前,如何使用 Windows API CreateProcess 和 WaitForSingleObject 函数等待,直到 Shelled 进程结束。

CreateProcess 函数的语法比较复杂,因此在示例代码中,该函数被封装在一个名为 ExecCmd 的函数中。 ExecCmd 利用一个参数(即应用程序的命令行)来执行。

  1. 创建一个标准模块,并将以下行粘贴到声明节:

    VB复制
    Option Explicit Private Type STARTUPINFO 
    cb As Long lpReserved As String lpDesktop As String lpTitle As String dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Integer cbReserved2 As Integer lpReserved2 As Long hStdInput As Long hStdOutput As Long hStdError As Long End Type Private Type PROCESS_INFORMATION 
    hProcess As Long hThread As Long dwProcessID As Long dwThreadID As Long End Type Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _ 
    hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function CreateProcessA Lib "kernel32" (ByVal _ 
    lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _ 
    lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _ ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _ ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _ 
    lpStartupInfo As STARTUPINFO, lpProcessInformation As _ 
    PROCESS_INFORMATION) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal _ 
    hObject As Long) As Long Private Const NORMAL_PRIORITY_CLASS = &H20& Private Const INFINITE = -1& 

  2. 将以下代码粘贴到模块中:

    VB复制
    Public Sub ExecCmd(cmdline As String) Dim proc As PROCESS_INFORMATION Dim start As STARTUPINFO Dim ReturnValue As Integer ' Initialize the STARTUPINFO structure:  start.cb = Len(start) ' Start the shelled application:  ReturnValue = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _ 
    NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc) ' Wait for the shelled application to finish:  Do ReturnValue = WaitForSingleObject(proc.hProcess, 0) 
    DoEvents Loop Until ReturnValue <> 258 ReturnValue = CloseHandle(proc.hProcess) End Sub Sub Testing() 
    ExecCmd "NOTEPAD.EXE" MsgBox "Process Finished" End Sub 

  3. Type Testing in the Immediate window. 记事本启动。 关闭记事本后, 会出现一个消息框, 通知您该过程已完成。