2014-11-05 344 views
1

我正在从Word 2013运行一个VBA宏。我试图运行一个需要参数/文件名参数的可执行文件。例如,c:\ myfilefilter.exe filetobefiltered.htm 我想使用Shell Run,因为我希望VBA代码等待可执行文件完成运行,然后再恢复VBA代码。下面的代码中的最后三行是我尝试过的一些不起作用的语法的示例。我认为问题是可执行程序和它过滤的文件之间的空间。是否有人知道正确的语法或等待可执行程序完成的方法。如果您需要更多信息,请询问。vba WScript.Shell运行.exe文件,参数为

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之后,第一个数字是一个boolean toogle 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 
+0

我不知道答案,并不一定要考一个好办法,但在你的上线,你应该删除所有引号,并加入Chr(34)和你的变量和符号。我假设你在某处定义了所有这些变量? – Christina 2014-11-06 20:12:39

回答

2

你可以试试这个。适用于我。

Const BatchFileName = "P:\Export.bat" 

昏暗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 
1

这对我的作品(从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"