vba WScript.Shell run .exe file with parameter

血红的双手。 提交于 2019-11-26 22:06:55

问题


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

回答1:


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



回答2:


this works for me (from 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"


来源:https://stackoverflow.com/questions/26762566/vba-wscript-shell-run-exe-file-with-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!