46

I have a target file path that is structured like example below.

C:\Program Files\Test\foobar.exe /G

What I need to do is be able to execute this file using VBA's shell() command.

How do I have to format the file path to tell Shell() that there is an argument that it needs to call along with running the .exe

What I've read/tried (with no avail) is below with the results to the right.

file = """C:\Program Files\Test\foobar.exe"" /G"    <---Bad file name or number (Error 52) 
shell(file)

file2 = "C:\Program Files\Test\foobar.exe /G"       <---file never found
shell(file2)

I've succeeded with running other .exe's using shell() so I know it's not a problem with VBA or the function.

Example:

works = "C:\Program Files\Test\test.exe"
shell(works)

I'm not particularly familiar with the process involved with executing files that require additional parameters so if I misspeak or you need more information, please let me know.

Teamothy
  • 2,000
  • 3
  • 16
  • 26
Austin A
  • 2,990
  • 6
  • 27
  • 42

4 Answers4

55

This works for me (Excel 2013):

Public Sub StartExeWithArgument()
    Dim strProgramName As String
    Dim strArgument As String

    strProgramName = "C:\Program Files\Test\foobar.exe"
    strArgument = "/G"

    Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)
End Sub

With inspiration from here https://stackoverflow.com/a/3448682.

Community
  • 1
  • 1
MBWise
  • 700
  • 1
  • 8
  • 17
  • 1
    This might also be of interest: [Run Associated Program](http://stackoverflow.com/a/18921970/2454604). – MBWise Jun 10 '14 at 15:19
  • 2
    Great job, I'm having a little difficulty wrapping my head around the escaping double quotes. Would you elaborate on this? I understand you must prepend double quotes with a pair of double quotes to escape them, so adding the space in the middle I get... But I don't get the beginning and end. I get that the Shell function must be passed a string, and that placing double quotes in front of the variable would cause the variable to be treated as a string... but why 4 sets of double quotes? – Kevin Scharnhorst Aug 06 '14 at 01:57
  • 1
    @KevinScharnhorst The 4 quotes at the beginning (and end) means 'a string with one quote', and the quotes between ampersands 2 and 3 means 'a string with two quotes and a space character in between'. This meens calling the Shell function with '"C:\Program Files\Test\foobar.exe" "/G"' (without the single quotes). – MBWise Aug 07 '14 at 09:58
  • This doesnt do it: "C:\Windows\System32\cacls.exe" "C:\Program Files (x86)\software foo\DataBase\DataBase.mdb" "/e /p Benutzer:F" any idea? – user3305711 Jan 22 '18 at 12:55
  • @user3305711 Make sure it runs in the command window first. Try different variations on quotation, e.g., "C:\Windows\System32\cacls.exe" ""C:\Program Files (x86)\software foo\DataBase\DataBase.mdb" /e /p Benutzer:F" – MBWise Jan 25 '18 at 09:09
14

Here are some examples of how to use Shell in VBA.
Open stackoverflow in Chrome.

Call Shell("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" & _
 " -url" & " " & "www.stackoverflow.com",vbMaximizedFocus)

Open some text file.

Call Shell ("notepad C:\Users\user\Desktop\temp\TEST.txt")

Open some application.

Call Shell("C:\Temp\TestApplication.exe",vbNormalFocus)

Hope this helps!

Teamothy
  • 2,000
  • 3
  • 16
  • 26
niklasolsn
  • 303
  • 5
  • 10
  • Can I ask what the significance of the vbMaximizedFocus vs. vbNormalizedFocus? Also, how do I work in a parameter that gets called along with the file? – Austin A Apr 15 '14 at 04:29
  • 1
    This just determines whether you want the program to open in a maximized window or in a normal size window. Concerning arguments I just think you do something like this: Shell (C:\SomeApplication argument1 argument 2). Also there is the ShellExecute function that provides more options to your call. – niklasolsn Jun 17 '14 at 15:24
1

The below code will help you to auto open the .exe file from excel...

Sub Auto_Open()


    Dim x As Variant
    Dim Path As String

    ' Set the Path variable equal to the path of your program's installation
    Path = "C:\Program Files\GameTop.com\Alien Shooter\game.exe"
    x = Shell(Path, vbNormalFocus)

End Sub
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Sameer
  • 21
  • 1
0
sTempBAT = "d:\tempLog.txt"    
Set shellwindows = GetObject("new:9ba05972-f6a8-11cf-a442-00a0c90a8f39")
Set itemobj = shellwindows.Item()
itemobj.document.Application.ShellExecute sTempBAT, "", "", "open", 0

An alternative way to call shell function https://blog.sevagas.com/IMG/pdf/bypass_windows_defender_attack_surface_reduction.pdf

Thanis Albert
  • 11
  • 1
  • 4