如何使用vba的shell()运行带参数的.exe?

时间:2014-01-04 05:30:51

标签: shell vba excel-vba parameters command-line-arguments

我有一个目标文件路径,结构如下所示。

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

我需要做的是能够使用vba的shell()命令执行此文件。

如何格式化文件路径以告诉Shell()有一个参数需要调用以及运行.exe

我的阅读/尝试(无效)下面是右边的结果。

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)

我已成功使用shell()运行其他.exe所以我知道它不是vba或函数的问题。

示例:

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

我对执行需要其他参数的文件的过程并不是特别熟悉,所以如果我错过了或者您需要更多信息,请告诉我。

4 个答案:

答案 0 :(得分:34)

这对我有用(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

灵感来自https://stackoverflow.com/a/3448682

答案 1 :(得分:7)

以下是如何在VBA中使用Shell的一些示例 在Chrome中打开stackoverflow。

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


打开一些文本文件。

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


打开一些申请。

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


希望这有帮助!

答案 2 :(得分:0)

以下代码将帮助您从excel ...

自动打开.exe文件
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

答案 3 :(得分: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

另一种调用shell函数的方式 https://blog.sevagas.com/IMG/pdf/bypass_windows_defender_attack_surface_reduction.pdf