Excel 如何使用vba';s shell()?

Excel 如何使用vba';s shell()?,excel,vba,shell,parameters,command-line-arguments,Excel,Vba,Shell,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)

我有一个目标文件路径,其结构如下面的示例所示

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)

我对执行需要额外参数的文件所涉及的过程不是特别熟悉,因此如果我说错了或您需要更多信息,请告诉我。

以下是一些如何在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)
希望这有帮助

这对我有用(Excel 2013):


下面的代码将帮助您从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
调用shell函数的另一种方法

这应该行得通。但是,作为一种解决方法,您可以使用创建并执行的批处理文件。@Olaf。以上两个例子是否都有效?我能问一下vbMaximizedFocus与vbNormalizedFocus的区别吗?另外,如何处理随文件一起调用的参数?这只是确定您希望程序在最大化窗口还是在正常大小窗口中打开。关于参数,我认为您可以这样做:Shell(C:\SomeApplication argument1参数2)。还有一个ShellExecute函数,它为您的调用提供了更多选项。这可能也是您感兴趣的:。做得好,我有点难以理解转义双引号。你能详细说明一下吗?我知道你必须用一对双引号准备双引号来逃避它们,所以在中间加上空间…但我不知道开头和结尾。我知道Shell函数必须传递一个字符串,在变量前面加双引号会导致变量被视为字符串。。。但是为什么有4组双引号?@KevinScharnhorst开头(和结尾)的4个引号表示“带一个引号的字符串”,而符号2和3之间的引号表示“带两个引号和一个空格字符的字符串”。这需要使用“C:\Program Files\Test\foobar.exe”“/G”(不带单引号)调用Shell函数。这样做不行:“C:\Windows\System32\cacls.exe”“C:\Program Files(x86)\software foo\DataBase\DataBase.mdb”“/e/p Benutzer:F”any idea?@user3305711请确保它首先在命令窗口中运行。请尝试引用不同的变体,例如“C:\Windows\System32\cacls.exe”“C:\Program Files(x86)\software foo\DataBase\DataBase.mdb”/e/p Benutzer:F”
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
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
sTempBAT = "d:\tempLog.txt"    
Set shellwindows = GetObject("new:9ba05972-f6a8-11cf-a442-00a0c90a8f39")
Set itemobj = shellwindows.Item()
itemobj.document.Application.ShellExecute sTempBAT, "", "", "open", 0