How do you run a .exe with parameters using vba's shell()?

ITISAGLN

New Member
Joined
Jan 23, 2015
Messages
6
Via VBA, I am trying to run an executable that has parameters and a file name.

Dim strProgramName As String
Dim strArgument0 As String
Dim strArgument1 As String

'//This is the executable
strProgramName = "C:\Dir0\Dir1\Dir2\Dir3\MyExecutable.exe"

'//These are the required parameters
strArgument0 = " /x /y "

'//This is file to process
strArgument1 = "C:\Dir0\Dir1\Dir2\Dir3\Dir4\Dir5\MyFile.TXT"

Dim LValue As String
LValue = """" & strProgramName & """ """ & strArgument0 & """ """ & strArgument1 & """"

Call Shell(LValue, vbNormalFocus)

This call works through windows command line and I am trying to get it to work when hitting a VBA button.
Command Line is something like this: " "C:\Dir0\Dir1\Dir2\Dir3\MyExecutable.exe /x /y "C:\Dir0\Dir1\Dir2\Dir3\Dir4\Dir5\MyFile.TXT" "

When the button is hit, this function is called that has Call Shell(LValue, vbNormalFocus)...but I get errors...the errors are relative to the executable with arguments and file so I think it's an escape, slash, file name type error
Any help is appreciated.
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Welcome to the forum, when you use shell command to an exe file you shouldn't need to include the path as Shell will look for the Application.exe file where ever it is located. if you look at this code snippet it might help.

Code:
Sub shel1()
Dim retval
retval = Shell("notepad.exe C:\Access\Simple.txt", vbNormalFocus)

End Sub
 
Upvote 0
Thanks for the response. I have many .exe's with the same name in different directories so I need to point to one of them. Then I have parameters to send to the .exe. Then the file name.
 
Upvote 0
This did it and now I can piece together...Thanks!
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1

wsh.Run "C:\Dir\Dir\Dir\My.exe /x /y C:\Dir\Dir\Dir\Dir\Dir\Dir\MyFile.txt", windowStyle, waitOnReturn
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top