Home > OS >  Running Exe File from CMD using VBA Excel
Running Exe File from CMD using VBA Excel

Time:04-21

I am trying to run a simple cmd line from VBA and I am having issues. Basically, the command line calls a createReport.exe which creates a final CSV output file using Inputfile.csv

This is what I run manually from Command prompt window :

cd C:\Users\user123\Desktop\MyReport_folder (hits enter)

createReport.exe -in=C:\Users\user123\Desktop\MyReport_folder\Inputfile.csv (hits enter)

When I run manually, everything works fine, it takes around 45 seconds to create final CSV output file. But when I run the same thing from vba macro , the screen says starting the query step and it stays on for 30 seconds, closes and doesn't create the final CSV output file.

Anything I am missing in my code here ?

Sub RunReport()
Application.DisplayAlerts = False

 Dim strProgramName As String
    Dim strArgument As String    
    
    strProgramName = "C:\Users\user123\Desktop\MyReport_folder\createReport.exe"
    strArgument = "-in=C:\Users\user123\Desktop\MyReport_folder\Inputfile.csv"

    Call Shell("""" & strProgramName & """ """ & strArgument & """", vbMaximizedFocus)

Application.DisplayAlerts = True

End Sub

This above query calls command prompt , runs, but stops after 30 seconds for the step 1 in the picture that says "Starting query step..." (whereas the process runs perfectly if I run it manually from cmd window)

enter image description here

Your help is appreciated. Thanks.

CodePudding user response:

I believe you first need to do a ChDir before calling the Shell() command, something like:

ChDir "C:\Users\user123\Desktop\MyReport_folder"
Call Shell("""" & strProgramName ...
  • Related