运行vba脚本时出现运行时错误'91'

时间:2019-07-05 00:59:32

标签: vba sap

我遇到了错误:

  

在行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

我希望它运行我打开的SAP。

2 个答案:

答案 0 :(得分:0)

错误91是您要使用的对象设置为“无”时。因此,尽管该行引发错误,但该行实际上没有任何错误。需要确定的是我们如何获取会话对象。

我对使用会话对象的偏爱是设置一个负责该功能的Function,然后在主Sub / Function中只有几行代码。我们的原始功能之一不是很漂亮,但是应该做您需要的事情……

    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

我希望这会有所帮助,并希望它能奏效。让我们知道你的情况。

答案 1 :(得分:0)

但是您也可以尝试以下操作:

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

关于, 脚本人

相关问题