vba WScript.Shell 运行带参数的 .exe 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26762566/
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 09:07:07  来源:igfitidea点击:

vba WScript.Shell run .exe file with parameter

vbaword-vbawsh

提问by user1892770

I am running a VBA macro from Word 2013. I'm trying to run an executable file which requires an argument/parameter of a filename. For example, c:\myfilefilter.exe filetobefiltered.htm

我正在运行 Word 2013 中的 VBA 宏。我正在尝试运行一个需要文件名参数/参数的可执行文件。例如, c:\myfilefilter.exe filetobefiltered.htm

I would like to use Shell Runbecause I want the VBA code to wait until the executable is finished running before resuming the VBA code. The last three lines in the code below are examples of some of the syntax I have tried which do not work. I think the problem is the space between the executable program and the file it filters. Does anybody know the correct syntax or a way to wait for the executable program to finish. If you need more info, please ask.

我想使用Shell Run是因为我希望 VBA 代码在恢复 VBA 代码之前等待可执行文件完成运行。下面代码中的最后三行是我尝试过的一些不起作用的语法示例。我认为问题在于可执行程序与其过滤的文件之间的空间。有没有人知道正确的语法或等待可执行程序完成的方法。如果您需要更多信息,请询问。

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim errorCode As Integer
Dim strProgramName As String
Dim strMyFile As String
Call Shell("""" & strProgramName & """ """ & strMyFile & """", vbNormalFocus, waitOnReturn"""")
wsh.Run(strProgramName & """ """ & fileToFilterB & """", vbNormalFocus, waitOnReturn)
wsh.Run "Chr(34)strProgramNameChr(34)strMyFile", waitOnReturn


The code below works. After strCMD, the first number is a boolean argument: 1 displays the dos box and 0 hides the dos box; the second number is similar: 1 waits for the program to finish before continuing the VBA code, 0 does not wait.

下面的代码有效。之后strCMD,第一个数字是一个布尔参数:1 显示 dos 框,0 隐藏 dos 框;第二个数字类似:1 在继续 VBA 代码之前等待程序完成,0 不等待。

strCMD = sMyProgram + " " + sMyFile
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
wsh.Run strCMD, 1, 1

回答by Webtopia

You could try this. Works for me.

你可以试试这个。对我来说有效。

Const BatchFileName = "P:\Export.bat"

Dim wsh As Object

Dim wsh 作为对象

Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1

wsh.Run BatchFileName, windowStyle, waitOnReturn

Kill BatchFileName

回答by Philippe Raemy

this works for me (from MsAccess 2013 VBA)

这对我有用(来自 MsAccess 2013 VBA)

Dim wShell As New WshShell
Dim wsExec As WshExec
    Dim cmdline As String
    cmdline = "notepad c:\somefile.txt"
    Debug.Print Now, cmdline
    Set wsExec = wShell.Exec(cmdline)
    Do While wsExec.Status = 0
        DoEvents
    Loop
    Debug.Print Now, "done"