Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

ms word - vba WScript.Shell run .exe file with parameter

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

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could try this. Works for me.

Const BatchFileName = "P:Export.bat"

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...