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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20917355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:30:43  来源:igfitidea点击:

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

excelvbashellparameterscommand-line-arguments

提问by Austin A

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.

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

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

我如何格式化文件路径以告诉Shell()它需要在运行 .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.

我已经成功地使用 shell() 运行了其他 .exe,所以我知道这不是 VBA 或函数的问题。

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.

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

回答by MBWise

This works for me (Excel 2013):

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

从这里获得灵感https://stackoverflow.com/a/3448682

回答by niklasolsn

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

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

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!

希望这可以帮助!

回答by Sameer

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

下面的代码将帮助您从 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