angad bector
angad bector

Reputation: 13

Command works in "Run" but not in "CMD" or "Call Shell"

I have a .exe file I want to run with some parameters, when I open "Run" (windows+R) and type in the following: C:\reporting\Release\Report_GenerationV0 Alarm 1-1-1 00:00:00 1-1-1 00:00:00 DTPUN02-01\ADMINISTRATOR, the program runs perfectly.

However if I try this same thing through CMD (ive tried CMD with cmd /c) or Call Shell() (I Ultimately need it to work in VBA Call Shell, it doesn't work.

Text Input in Run: C:\reporting\Release\Report_GenerationV_0.exe Alarm 1-1-1 00:00:00 1-1-1 00:00:00 DTPUN02-01\ADMINISTRATOR - WORKS

Text Input in CMD: C:\reporting\Release\Report_GenerationV_0.exe Alarm 1-1-1 00:00:00 1-1-1 00:00:00 DTPUN02-01\ADMINISTRATOR -- DOESN'T WORK

Text Input in CMD: cmd /c C:\reporting\Release\Report_GenerationV_0.exe Alarm 1-1-1 00:00:00 1-1-1 00:00:00 DTPUN02-01\ADMINISTRATOR -- DOESN'T WORK

Call Shell Command: Call Shell("C:\reporting\Release\Report_GenerationV_0.exe Alarm 1-1-1 00:00:00 1-1-1 00:00:00 DTPUN02-01\ADMINISTRATOR") -- DOESN'T WORK

There are no errors in CMD or in Call Shell, the cursor shows a loading icon for some time and then it stops, nothing happens, ideally at the end of the execution of my program a PDF should be created and opened

Im completely confused by this...

Upvotes: 1

Views: 713

Answers (1)

Pᴇʜ
Pᴇʜ

Reputation: 57683

I cannot reproduce your issue. This should work. Your issues looks to be somewhere else or in your exe file:

I tested with the following code

Sub test()
    Call Shell("D:\temp\test.bat Alarm 1-1-1 00:00:00 1-1-1 00:00:00 DTPUN02-01\ADMINISTRATOR", vbNormalFocus)
End Sub

and the following test.bat to list the parameters:

@echo off
setlocal enabledelayedexpansion

set argCount=0
for %%x in (%*) do (
   set /A argCount+=1
   set "argVec[!argCount!]=%%~x"
)

echo Number of processed arguments: %argCount%

for /L %%i in (1,1,%argCount%) do echo %%i- "!argVec[%%i]!"

pause

The outcome is this:

enter image description here

If you have any spaces in your path make sure to surround it by quotes like below:

Call Shell("""D:\temp\test.bat"" Alarm 1-1-1 00:00:00 1-1-1 00:00:00 DTPUN02-01\ADMINISTRATOR", vbNormalFocus)

Also try the following to keep the cmd window open and see errors:

Call Shell("cmd /k ""C:\reporting\Release\Report_GenerationV_0.exe"" Alarm 1-1-1 00:00:00 1-1-1 00:00:00 DTPUN02-01\ADMINISTRATOR")

Upvotes: 1

Related Questions