2

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 Answer 1

3

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 ...
Sign up to request clarification or add additional context in comments.

3 Comments

So, will it be something like this ? Dim strProgramName As String Dim strArgument As String ChDir "C:\Users\user123\Desktop\MyReport_folder" strProgramName = "C:\Users\user123\Desktop\MyReport_folder\createReport.exe" strArgument = "-in=C:\Users\user123\Desktop\MyReport_folder\Inputfile.csv" Call Shell("""" & strProgramName & """ """ & strArgument & """", vbMaximizedFocus)
No, there's no chDir in your code.
That is so awesome. Thanks Dominique. I added exactly what you said and it spit out the output csv. This was my first time using VBA for cmd line. It worked now. 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" ChDir "C:\Users\user123\Desktop\MyReport_folder" Call Shell("""" & strProgramName & """ """ & strArgument & """", vbMaximizedFocus)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.