VBScript - How to make program wait until process has finished?

后端 未结 4 1383
深忆病人
深忆病人 2020-12-02 01:54

I have a problem in a VBScript that I am using with a VBA/Excel macro and a HTA. The problem is just the VBScript, I have the other two components, i.e. the VBA macro and HT

相关标签:
4条回答
  • 2020-12-02 02:21

    This may not specifically answer your long 3 part question but this thread is old and I found this while searching today. Here is one shorter way to: "Wait until a process has finished." If you know the name of the process such as "EXCEL.EXE"

    strProcess = "EXCEL.EXE"    
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = '"& strProcess &"'")
    Do While colProcesses.Count > 0
        Set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = '"& strProcess &"'")
        Wscript.Sleep(1000) 'Sleep 1 second
       'msgbox colProcesses.count 'optional to show the loop works
    Loop
    

    Credit to: http://crimsonshift.com/scripting-check-if-process-or-program-is-running-and-start-it/

    0 讨论(0)
  • 2020-12-02 02:24

    You need to tell the run to wait until the process is finished. Something like:

    const DontWaitUntilFinished = false, ShowWindow = 1, DontShowWindow = 0, WaitUntilFinished = true
    set oShell = WScript.CreateObject("WScript.Shell")
    command = "cmd /c C:\windows\system32\wscript.exe <path>\myScript.vbs " & args
    oShell.Run command, DontShowWindow, WaitUntilFinished
    

    In the script itself, start Excel like so. While debugging start visible:

    File = "c:\test\myfile.xls"
    oShell.run """C:\Program Files\Microsoft Office\Office14\EXCEL.EXE"" " & File, 1, true
    
    0 讨论(0)
  • 2020-12-02 02:38
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
    objWMIService.Create "notepad.exe", null, null, intProcessID
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colMonitoredProcesses = objWMIService.ExecNotificationQuery _
        ("Select * From __InstanceDeletionEvent Within 1 Where TargetInstance ISA 'Win32_Process'")
    Do Until i = 1
        Set objLatestProcess = colMonitoredProcesses.NextEvent
        If objLatestProcess.TargetInstance.ProcessID = intProcessID Then
            i = 1
        End If
    Loop
    Wscript.Echo "Notepad has been terminated."
    
    0 讨论(0)
  • 2020-12-02 02:39

    Probably something like this? (UNTESTED)

    Sub Sample()
        Dim strWB4, strMyMacro
        strMyMacro = "Sheet1.my_macro_name"
    
        '
        '~~> Rest of Code
        '
    
        'loop through the folder and get the file names
        For Each Fil In FLD.Files
            Set x4WB = x1.Workbooks.Open(Fil)
            x4WB.Application.Visible = True
    
            x1.Run strMyMacro
    
            x4WB.Close
    
            Do Until IsWorkBookOpen(Fil) = False
                DoEvents
            Loop
        Next
    
        '
        '~~> Rest of Code
        '
    End Sub
    
    '~~> Function to check if the file is open
    Function IsWorkBookOpen(FileName As String)
        Dim ff As Long, ErrNo As Long
    
        On Error Resume Next
        ff = FreeFile()
        Open FileName For Input Lock Read As #ff
        Close ff
        ErrNo = Err
        On Error GoTo 0
    
        Select Case ErrNo
        Case 0:    IsWorkBookOpen = False
        Case 70:   IsWorkBookOpen = True
        Case Else: Error ErrNo
        End Select
    End Function
    
    0 讨论(0)
提交回复
热议问题