Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a command in command prompt using excel VBA

I have a fixed command which i need to pass to command prompt using VBA and then the command should run. e.g. "perl a.pl c:\temp"

following is the command i am trying to use but it just opens command prompt and doesn't run the command.

Call Shell("cmd.exe -s:" & "perl a.pl c:\temp", vbNormalFocus)

Please check.

like image 621
user1699227 Avatar asked Jul 30 '13 20:07

user1699227


People also ask

How do you call a command button in VBA?

Select the Button Form Control from the menu. Right click and hold the mouse then drag and release to create your button. The Assign Macro window will pop up and you can select the VBA procedure you want to run from the button. Press the OK button.


1 Answers

The S parameter does not do anything on its own.

/S      Modifies the treatment of string after /C or /K (see below)  /C      Carries out the command specified by string and then terminates   /K      Carries out the command specified by string but remains   

Try something like this instead

Call Shell("cmd.exe /S /K" & "perl a.pl c:\temp", vbNormalFocus) 

You may not even need to add "cmd.exe" to this command unless you want a command window to open up when this is run. Shell should execute the command on its own.

Shell("perl a.pl c:\temp") 



-Edit-
To wait for the command to finish you will have to do something like @Nate Hekman shows in his answer here

Dim wsh As Object Set wsh = VBA.CreateObject("WScript.Shell") Dim waitOnReturn As Boolean: waitOnReturn = True Dim windowStyle As Integer: windowStyle = 1  wsh.Run "cmd.exe /S /C perl a.pl c:\temp", windowStyle, waitOnReturn 
like image 50
Ripster Avatar answered Sep 18 '22 01:09

Ripster