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

后端 未结 4 1399
深忆病人
深忆病人 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: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
    

提交回复
热议问题