【问题标题】:Run time error '91' when SAP connection is emptySAP 连接为空时运行时错误“91”
【发布时间】:2019-07-05 00:59:32
【问题描述】:

我遇到了错误:

“对象变量或未设置块变量”

一行

session.findById("wnd[0]").maximize

我在网上搜索,但错误在不同的行。

Sub RunScript()

Dim session As Object

If Not IsObject(Sapplication) Then
    Set SapGuiAuto = GetObject("SAPGUI")
    Set Sapplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
    Set Connection = Sapplication.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 Sapplication, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/NME21N"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/btn[12]").press

End Sub

我进去了,发现connection是空的,报错

“集合的枚举器找不到具有特定索引的元素”

【问题讨论】:

  • 我总是使用相同的行来最大化窗口,所以不要认为这会是一个特别的问题。你在设置会话对象吗?你能观察会话对象吗?当它出错时,你可以看到它确实存在。无论您在哪个屏幕上,我还使用 session.StartTransaction “ME21N” 开始新事务。
  • 所以我尝试进入,我发现连接是空的,它给出了这样的错误“集合的枚举器找不到具有特定索引的元素”
  • 因此错误出现在您尝试捕获会话对象的第一部分。运行宏时您的 SAP 窗口是否已经打开?
  • 是的,它在我运行宏时打开。我也在 SAP 中启用了脚本。
  • 你好,我想问一下,有没有可能是RZ11引起了所有这些问题?因为我无权访问 RZ11,因此我无法检查脚本选项。

标签: vba sap


【解决方案1】:

错误 91 是您尝试使用的 Object 设置为 Nothing。因此,尽管该行抛出错误,但该行实际上并没有错。需要解决的是我们如何获取会话对象。

我对使用会话对象的偏好是设置一个处理该问题的函数,然后在您的主子/函数中只需要几行代码。我们最初的功能之一并不漂亮,但应该做你需要的事情是这样的......

    Public session As Object

Function GetSession() As Boolean
    Dim IE As InternetExplorerMedium
    Dim Created_IE As Boolean
    Dim Sh As New Shell32.Shell
    Dim ShellWindow As Object
    Dim OpenWindows As Integer: OpenWindows = 0
    Dim OpenSessions As Integer
    Dim LoopCount As Integer: LoopCount = 0
    Dim SAPSession_Known As Boolean: SAPSession_Known = False
    Dim SAPGuiScripting As Object
    Dim SAPApplication As Object
    Dim SAPConnection As Object
    Dim SAPSession As Object

    ' Loop through all open windows to bring the SAP Portal into focus
    For Each ShellWindow In Sh.Windows
        ' Find the Window with the desired SAP Portal URL
        If InStr(1, LCase$(ShellWindow.LocationURL), "com.sap.portal.appintegrator.sap.Transaction") Then
            ' Count the number of open SAP Sessions
            OpenWindows = OpenWindows + 1
        End If
DoEvents
    Next ShellWindow


    Do Until SAPSession_Known = True Or LoopCount > 100
On Error Resume Next
        Set SAPGuiScripting = GetObject("SAPGUI")
        Set SAPApplication = SAPGuiScripting.GetScriptingEngine

On Error GoTo 0
        If OpenWindows = 0 Then

On Error Resume Next
            Set SAPConnection = SAPApplication.Children(0)
On Error GoTo 0
On Error Resume Next
            Set SAPSession = SAPConnection.Children(0)
On Error GoTo 0

        Else

On Error Resume Next
            Set SAPConnection = SAPApplication.Children(Int(OpenWindows - 1))
On Error GoTo 0
On Error Resume Next
            Set SAPSession = SAPConnection.Children(Int(OpenWindows - 1))
On Error GoTo 0

        End If
DoEvents
On Error Resume Next
        OpenSessions = SAPApplication.Children.Count
On Error GoTo 0

        If (OpenSessions <> 0) And Not (SAPSession Is Nothing) Then SAPSession_Known = True
    Loop

On Error Resume Next
    AppActivate ("com.sap.portal.appintegrator.sap.Transaction")
On Error GoTo 0

    If SAPSession Is Nothing Then
        MsgBox "SAP session not found"
        GetSession = False
    Else
        Set session = SAPSession
        GetSession = True
    End If
End Function

然后我将通过检查是否已设置 SAP 会话来启动我的宏,如果没有找到它,然后再继续执行程序的其余部分。如果一个不存在,它将结束程序。

Sub RunScript()
    Dim SAPOpen As Boolean

    If session Is Nothing Then
        SAPOpen = SAPGetSession
        If SAPOpen = False Then
            MsgBox "No SAP session could be found." & vbNewLine & _
                    "Open a SAP session and try again."
            End
        End If
    End If

    With session
        .findById("wnd[0]").maximize
        .StartTransaction "ME21N"     ' I prefer this for starting new transactions
        '.findById("wnd[0]").sendVKey 0   ' and it means we can avoid this line
        .findById("wnd[0]/tbar[0]/btn[12]").press
    End With
End Sub

我希望这会有所帮助,我希望这有效。让我们知道你的进展。

【讨论】:

    【解决方案2】:

    但您也可以尝试以下方法:

    Sub RunScript()
    
    'Dim session As Object
    
    'If Not IsObject(Sapplication) Then
    Set SapGuiAuto = GetObject("SAPGUI")
    Set Sapplication = SapGuiAuto.GetScriptingEngine
    'End If
    'If Not IsObject(Connection) Then
    con = 0
    Set Connection = Sapplication.Children(Int(con))
    'End If
    'If Not IsObject(session) Then
    ses = 0
    Set session = Connection.Children(Int(ses))
    'End If
    'If IsObject(WScript) Then
    'WScript.ConnectObject session, "on"
    'WScript.ConnectObject Sapplication, "on"
    'End If
    session.findById("wnd[0]").maximize
    session.findById("wnd[0]/tbar[0]/okcd").Text = "/NME21N"
    session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]/tbar[0]/btn[12]").press
    
    End Sub
    

    问候, 脚本人

    【讨论】:

    • 对我不起作用,Set session = connection.children(0) 出现错误“集合的枚举器找不到具有特定索引的元素。”
    • 如果上面的更改能做点什么会很有趣。
    猜你喜欢
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 2013-04-16
    • 1970-01-01
    相关资源
    最近更新 更多