vba WScript.Shell 运行带参数的 .exe 文件 [英] vba WScript.Shell run .exe file with parameter

查看:352
本文介绍了vba WScript.Shell 运行带参数的 .exe 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I would like to use Shell Run because 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.

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

<小时>

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


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 = sMyProgram + " " + sMyFile
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
wsh.Run strCMD, 1, 1

推荐答案

你可以试试这个.对我有用.

You could try this. Works for me.

Const BatchFileName = "P:\Export.bat"

将 wsh 调暗为对象

Dim wsh As Object

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

这篇关于vba WScript.Shell 运行带参数的 .exe 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆