Running Exe File from CMD using VBA Excel

Viewed 161

I am trying to run a cmd line from VBA. 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, it takes around 45 seconds to create final CSV output file.

When I run the same thing from VBA code, the screen says "starting the query step" and it stays on for 30 seconds, closes and doesn't create the final CSV output file.

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

enter image description here

1 Answers

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