【问题标题】:How to trap any VBS error and return it to the calling VBA in Access如何捕获任何 VBS 错误并将其返回给 Access 中的调用 VBA
【发布时间】:2021-11-11 07:30:09
【问题描述】:

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

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

我的目标是,如果 SAP 在运行 .VBS 时出现错误,那么它应该关闭活动的 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

【问题讨论】:

    标签: vba ms-access vbscript sap-gui


    【解决方案1】:

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

    .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
    

    编辑 - 要捕获您的 Status 变量,使其在脚本中成为全局变量(在子/函数之外声明)并使用 .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
    

    【讨论】:

    • 有第三方64位替换:Tablacus Script Control 64.
    • @Gustav 感谢您提供 64 位信息! :)
    • 嗨,我的 .vbs 文件中有一个字符串:Status = "Incompleted"。是否可以在使用 Err.Description 调用 VBA 时提取字符串 Status 的这个值?例如。出错时,我想提取 err.description 和此状态字符串。谢谢
    • 添加了阅读Status的示例。
    • @Alex K. 很棒的信息,效果很好,但我身边可悲的是,我还想在 .Vbs 文件中保留错误处理以关闭 SAP 的活动会话,以及当 .VBS 中的这一行时“On error resume next”打开,则 VBA 中的错误处理不起作用!因此我不能同时提取错误信息和状态字符串!是否可以在 .vbs 中保持错误处理,并将字符串从 .VBS 文件拉到调用 vba?例如。 On error resume next and If Err.Number 0 Then Status = "Script Error" ,然后关闭会话。谢谢
    猜你喜欢
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    相关资源
    最近更新 更多