2014-11-05 136 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"