簡體   English   中英

如何使用vba的shell()運行帶參數的.exe?

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

我有一個目標文件路徑,其結構如下所示。

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)

我已成功運行其他.exe使用shell()所以我知道它不是vba或函數的問題。

例:

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

我對執行需要其他參數的文件的過程並不是特別熟悉,所以如果我錯過了或者您需要更多信息,請告訴我。

這對我有用(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

以下是如何在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自動打開.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM