5

我正在使用以下Shell功能从 VBA 启动批处理脚本:

myRes = Shell("myScript.cmd")

有没有办法知道它是否运行成功或是否有执行错误?

4

2 回答 2

5

我建议您尝试使用WshShell对象而不是本机 Shell 函数。

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1 'or whatever suits you best
Dim errorCode As Integer

errorCode = wsh.Run("myScript.cmd", windowStyle, waitOnReturn)

If errorCode = 0 Then
    MsgBox "Execution successful. No error to report."
Else
    MsgBox "Program exited with error code " & errorCode & "."
End If    

虽然请注意:

如果bWaitOnReturn设置为 false(默认),则 Run 方法在启动程序后立即返回,自动返回 0(不被解释为错误代码)。

所以要检测程序是否执行成功,你需要waitOnReturn像我上面的例子一样设置为True。否则无论如何它都会返回零。

我的这个较早的答案也可能会有所帮助。

于 2012-08-15T07:35:45.580 回答
3

如果您认为该命令将在成功时返回 0,则可以捕获错误级别:http: //www.devx.com/vb2themax/Tip/18663

获取进程的退出码

在少数情况下,特别是在从 VB 应用程序中运行 MsDos 批处理文件时,您可能希望ERRORLEVEL通过外部应用程序确定集合。你不能用一个简单的 Shell 语句来做,但是有了GetProcessExitCodeAPI 函数的支持,这项工作就变得容易了:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
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
Const STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400


Private Sub cmdRunNotepad_Click()
    Dim hTask As Long
    Dim hProcess As Long
    Dim exitCode As Long

    hTask = Shell("Notepad", vbNormalFocus)
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, hTask)

    ' loop until the process returns a valid exit code
    Do
        ' relinquish this CPU time slice
        Sleep 100
        DoEvents
        ' query for exit code
        GetExitCodeProcess hProcess, exitCode
    Loop While exitCode = STILL_ACTIVE

    MsgBox "Exit code = " & exitCode, vbInformation


End Sub

弗朗切斯科·巴莱纳

或者你可以尝试这样的事情:

myRes = Shell("cmd /c myScript.cmd&&echo success")

这里有更多关于条件执行的信息:http ://www.robvanderwoude.com/condexec.php

但在这两种情况下,您都依赖退出代码。

于 2012-08-14T09:26:28.313 回答