Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7838291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:03:32+00:00 2026-06-02T15:03:32+00:00

I posted earlier about getting my VBScript to wait until a process had finished

  • 0

I posted earlier about getting my VBScript to wait until a process had finished before continuing (further info: VBScript – How to make program wait until process has finished?.

I was given an adequate answer after some discussion. However, it seems that I am now going in a new direction with the code as the solution presented another problem that I am hoping you may be able to help me with.

Basically I have some code which I have provided below. It takes in 4 arguments, one of which is a PATH to a folder containing many files which I want to use along with the other three in my VBA macro.

If WScript.Arguments.Count = 4 Then

    ' process input argument
    Set args = WScript.Arguments
    arg1 = args.Item(0)
    arg2 = args.Item(1)
    arg3 = args.Item(2)
    arg4 = args.Item(3)

    ' Create a WshShell instance 
    Dim WShell
    Set WShell = CreateObject("WScript.Shell")

    ' Create an Excel instance
    Dim x1
    Set x1 = CreateObject("Excel.Application")

    ' Disable Excel UI elements
    x1.DisplayAlerts = False
    x1.AskToUpdateLinks = False
    'x1.AlertBeforeOverwriting = False
    x1.FeatureInstall = msoFeatureInstallNone

    ' Open the Workbooks specified on the command-line
    Dim x1WB 
    Dim x2WB 
    Dim x3WB 
    Dim x4WB 
    Dim strWB1
    Dim strWB2
    Dim strWB3
    Dim strWB4

    Dim FSO
    Dim FLD
    Dim FIL
    Dim strFolder

    strWB1 = arg1
    Set x1WB = x1.Workbooks.Open(strWB1)
    ' Show the workbook/Excel program interface. Comment out for silent running.
    x1WB.Application.Visible = True

    strWB2 = arg2
    Set x2WB = x1.Workbooks.Open(strWB2)
    ' Show the workbook/Excel program interface. Comment out for silent running.
    x2WB.Application.Visible = True

    strWB3 = arg3
    Set x3WB = x1.Workbooks.Open(strWB3)
    ' Show the workbook/Excel program interface. Comment out for silent running.
    x3WB.Application.Visible = True

    'To hold the string of the PATH to the multiple files
    strFolder = arg4

    Set FSO = CreateObject("Scripting.FileSystemObject")
    'Get a reference to the folder I want to search
    set FLD = FSO.GetFolder(strFolder)

    Dim strMyMacro
    strMyMacro = "my_excel_sheet_with_vba_module.xlsm!Sheet1.my_vba_macro"

    'loop through the folder and get the file names
    For Each Fil In FLD.Files

        WshShell.run """C:\Program Files\Microsoft Office\Office14\EXCEL.exe"" " & Fil, 1, true

        x1.Run strMyMacro

        '~~> Problem - How do I get the macro to run before opening the above file but run after it has opened (due to setting the bWaitOnReturn to true)
        '~~> Problem - How do I get the file on current iteration to close after the macro has completed?
        '~~> Problem - If this is not the issue, can you identify it?

    Next

    x1WB.close
    x2WB.close
    x3WB.close
    'x4WB.close

    ' Clean up and shut down
    Set x1WB = Nothing
    Set x2WB = Nothing
    Set x3WB = Nothing
    Set x4WB = Nothing

    Set FSO = Nothing
    Set FLD = Nothing

    x1.Quit
    Set x1 = Nothing
    Set WshShell = Nothing

    WScript.Quit 0

Else
        WScript.Quit 1

End If

The script works like this:

  1. 4 arguments are passed to the script. The 3rd argument is a .xlsm file which contains my VBA macro. The last argument is a PATH to a folder containing multiple files.
  2. It then opens up the first three Excel files.
  3. Then I run a loop to iterate through the files Fil in the folder that was specified as the 4th argument. AFAIK this has to be done via a WScript.shell using the .run method so that the rest of the script will hang until the Excel file it is processing finishes before closing it and opening up the next file in the folder.
  4. After opening up file Fil, I then run the macro (albeit at this moment in time unsuccessfully).

I was tempted to simply open up all of the Excel files using the WScript.shell object however AFAIK I would not be able to run the macro this way.

Hopefully I have been able to define my aims of this piece of VBScript though if I haven’t let me know and I shall clarify. Can you help?

Thanks,
QF.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:03:34+00:00Added an answer on June 2, 2026 at 3:03 pm

    Something along these lines might work for you (in Excel). A few things I’m not clear on though:

    1. Where is your existing VBA macro – I’m guessing it’s in one of the 3 files you’re opening?
    2. What types of files are in the folder you’re looping through? I guessed Excel.
    3. How is the vbscript being run? It looks like you’re shelling out from your HTA, but why not include it directly in the HTA? That would save you from having to shell out and pass arguments…


    Option Explicit

    Dim wb1 As Workbook, wb2 As Workbook
    
    Sub ProcessFolder(path1, path2, sFolder)
    
        Dim wb As Workbook
        Dim s
    
        Set wb1 = Workbooks.Open(path1)
        Set wb2 = Workbooks.Open(path2)
        If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"
    
        s = Dir(sFolder & "*.xls*", vbNormal)
    
        Do While Len(s) > 0
            Set wb = Workbooks.Open(sFolder & s)
            ProcessFile wb
            wb.Close False
            s = Dir()
        Loop
    
        wb1.Close False
        wb2.Close False
    
    End Sub
    
    
    Sub YourExistingMacro(wb As Workbook)
    'do stuff with wb and presumably the other 3 open files...
    End Sub
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I posted another question earlier about getting the arguments from a context menu App.
I posted earlier today about an error I was getting with using the predict
Already i posted my earlier question about my FTP process (simple and short ),it
I posted earlier (and received valid answers, thanks) about a problem I had loading
I had posted another question earlier about deployment with Passenger. That problem turned out
I posted earlier today about template classes, but was pretty far off and got
This simple problem is kiling me. I posted something earlier about trying to clean
I posted a question earlier about stopping all sounds in a swf. Now, to
I posted a thread about this earlier and have made some progress but now
I had posted this earlier on Stack Overflow, but couldn't get a positive result.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.