2013-04-11 158 views
17

我跑在Excel VBA中一個簡單的shell命令運行象下面這樣的指定目錄下的批處理文件:等待shell命令來完成

Dim strBatchName As String 
strBatchName = "C:\folder\runbat.bat" 
Shell strBatchName 

有時批處理文件可能需要更長一些計算機上運行,並且正在執行的VBA代碼依賴於批處理文件來完成運行。我知道你可以設置一個等待計時器,如下所示:

Application.Wait Now + TimeSerial(0, 0, 5) 

但是,這可能不適用於某些計算機太慢。有沒有辦法系統地告訴Excel繼續執行其餘的VBA代碼,直到 shell運行完畢後?

+0

請參閱http://stackoverflow.com/a/1439241/1176601(WaitForSingleObject) – Aprillion 2013-04-11 14:51:43

回答

39

使用WScript.Shell相反,因爲它有一個waitOnReturn選項:

Dim wsh As Object 
Set wsh = VBA.CreateObject("WScript.Shell") 
Dim waitOnReturn As Boolean: waitOnReturn = True 
Dim windowStyle As Integer: windowStyle = 1 

wsh.Run "C:\folder\runbat.bat", windowStyle, waitOnReturn 

(IDEA從Wait for Shell to finish, then format cells - synchronously execute a command複製)

+1

in .net,但不是在VBA中...... – 2013-04-11 14:55:49

+1

好了,修改爲使用WScript.Shell。 – 2013-04-11 15:08:59

+0

自2010年升級到2013年(並將.xlam文件移動到C:\ Program Files \ Microsoft Office 15 \ root \ office15 \ Library \)後,我無法再使用此方法啓動.exe文件。我假設有一些軟文件權限 - 但我已經明確地將自己添加爲完全控制權限(我也是管理員的成員,它似乎已經擁有完整的權限),但仍然出現錯誤:應用程序無法正確啓動(0xc000007b)。單擊確定關閉該應用程序。任何想法/建議? – Terry 2013-09-27 15:38:16

0

您可以讓BATCH在創建文件時創建一個文件,VBA等待該文件的創建?或者在完成後批量刪除標誌文件,VBA等待標誌文件消失?

0

這是我用有VB等待過程完成後再繼續。 我沒有寫這個,不信任。

它在其他一些公開論壇提供和工作對我非常好:

需要的RunShell子程序以下聲明:

Option Explicit 

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long 

Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long 

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long 

Private Const PROCESS_QUERY_INFORMATION = &H400 

Private Const STATUS_PENDING = &H103& 

'then in your subroutine where you need to shell: 

RunShell (path and filename or command, as quoted text) 
+1

此代碼在VBA中不起作用,因爲沒有定義RunShell – CrazyHorse 2014-05-08 16:07:48

-2

昏暗WSH新的WshShell

CHDIR 「批處理文件目錄」

wsh。運行「批處理文件的完整路徑」,vbnormalfocus,真正

做兒子

+0

不理解您的答案.... – 2014-05-08 16:23:57

1

你與在運行命令括號的變化提出了工作正常使用VBA我

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 
wsh.Run "C:\folder\runbat.bat", windowStyle, waitOnReturn 
2

保存在bat文件 「C:\ WINDOWS \ SYSTEM32」,並使用下面的代碼,它正在

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 

    errorCode = wsh.Run("runbat.bat", windowStyle, waitOnReturn) 

If errorCode = 0 Then 
    'Insert your code here 
Else 
    MsgBox "Program exited with error code " & errorCode & "." 
End If 
4

添加以下子:

Sub SyncShell(ByVal Cmd As String, ByVal WindowStyle As VbAppWinStyle) 
VBA.CreateObject("WScript.Shell").Run Cmd, WindowStyle, True 
End Sub 

如果你添加一個引用C:\Windows\system32\wshom.ocx你也可以使用:

Sub SyncShell(ByVal Cmd As String, ByVal WindowStyle As VbAppWinStyle) 
Static wsh As New WshShell 
wsh.Run Cmd, WindowStyle, True 
End Sub 

這個版本應該是更有效的。