如何捕获任何 VBS 错误并将其 return 发送到 Access 中的调用 VBA

How to trap any VBS error and return it to the calling VBA in Access

我在 Microsoft Access 中有一个程序。我有 VBS 脚本文件来自动化 SAP GUI 屏幕(“事务”)。在 Access 中使用 VBA 使用 Scriptcontrol 对象打开这些不同的 VBS 脚本文件并在 SAP 系统中执行事务。

现在,有时在 运行 处理事务时出现错误,然后脚本停止。我已经在每个 VBS 脚本文件中编写了错误处理程序。

我的目标是,如果在 运行ning .VBS 时 SAP 出现错误,那么它应该关闭活动的 SAP 会话并将状态信息存储在一个名为“ScriptStatus”的字符串。然后我将这个字符串拉回调用 vba 来回 运行 相同的 .vbs 脚本。

.VBS 中的代码

    dim ScriptStatus
    
Function (DoWork) 
   
    If Not IsObject(application) Then
       Set SapGuiAuto  = GetObject("SAPGUI")
       Set application = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(connection) Then
       Set connection = application.Children(0)
    End If
    If Not IsObject(session) Then
       Set session    = connection.Children(0)
    End If
    If IsObject(WScript) Then
       WScript.ConnectObject session,     "on"
       WScript.ConnectObject application, "on"
    End If
    
    
    
    on error resume Next
    
    'SAP Code
    session.findById("wnd[0]").maximize
    'Furhter SAP Code
    'Change the ScriptStatus to completed
     ScriptStatus = "Script Completed"
    
    If Err.Number <> 0 Then
    'Change ScriptStatus
    ScriptStatus = "Script Error"
    'Close SAP Session
    session.findById("wnd[0]").Close
    End If

End Function

调用中的代码VBA

Sub Foo()
    Dim vbsCode As String, result As Variant, script As Object, ScriptInfo As String
    
    ReRunScript:

    '// load vbs source
    Open "x.vbs" For Input As #1
    vbsCode = Input$(LOF(1), 1)
    Close #1
    
    On Error GoTo ERR_VBS
    
    Set script = CreateObject("ScriptControl")
    script.Language = "VBScript"
    script.AddCode vbsCode
        
    result = script.Run("DoWork")
    ScriptInfo = script.Eval("ScriptStatus")
    If ScriptInfo = "Script Completed" Then 
    Exit Sub
    Elseif ScriptInfo = "Script Error" Then
    Goto ReRunScript
    End if

ERR_VBS:
    MsgBox Err.Description
 
    MsgBox script.Eval("ScriptStatus")
End Sub

而不是通过 cscript 运行 它们,您可以使用 ScriptControl(仅限 32 位)直接执行它们 - 这将使您可以使用标准 On Error 直接在 Access 中捕获错误(以及允许您捕获 return 值)。

示例 .VBS 文件:

function DoWork
    '// do some work
    msgbox 1
    '// error
    x = 100 / 0
    DoWork = "OK"
end function

VBA:

Sub Foo()
    Dim vbsCode As String, result As Variant
    
    '// load vbs source
    Open "x.vbs" For Input As #1
    vbsCode = Input$(LOF(1), 1)
    Close #1
    
    On Error GoTo ERR_VBS
    
    With CreateObject("ScriptControl")
        .Language = "VBScript"
        .AddCode vbsCode
        result = .Run("DoWork")
    End With
    
    Exit Sub

ERR_VBS:
    MsgBox Err.Description
End Sub

Edit - 要捕获您的 Status 变量,使其在脚本中成为全局变量(在 sub/function 之外声明)并使用 .Eval() 方法读入 VBA.

示例 .VBS 文件:

dim Status

function DoWork
    '// do some work
    msgbox 1

    Status = "Hello World"
    
    '// error 
    x = 100 / 0
    DoWork = "OK"
end function

VBA:

Sub Foo()
    Dim vbsCode As String, result As Variant, script As Object
    
    '// load vbs source
    Open "x.vbs" For Input As #1
    vbsCode = Input$(LOF(1), 1)
    Close #1
    
    On Error GoTo ERR_VBS
    
    Set script = CreateObject("ScriptControl")
    script.Language = "VBScript"
    script.AddCode vbsCode
        
    result = script.Run("DoWork")
    
    Exit Sub

ERR_VBS:
    MsgBox Err.Description
    '// read VBS global
    MsgBox script.Eval("Status")
End Sub