0

I met error :

Run-time error '91' Object Variable or with block variable not set

at line session.findById("wnd[0]").maximize below.

I searched online, but errors were at different lines.

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

I stepped into, and found that connection is empty, it gave a error like

The enumerator of the collection cannot find an element with the specific index

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
rob
  • 17
  • 6
  • I always use the identical line to maximise the window so don’t think that would be specifically a problem. Are you setting the session object? Can you watch the session object and when it errors see that it is indeed there. I also use session.StartTransaction “ME21N” for starting a new transaction no matter what screen you are on. – Josh Jul 05 '19 at 01:54
  • So i tried to step into, and i found out that connection is empty, it gave a error like this "the enumerator of the collection cannot find an element with the specific index" – rob Jul 05 '19 at 02:02
  • So the error is in the first section where you are trying to capture the session object. Is your SAP window already open when you run the macro? – Josh Jul 05 '19 at 02:06
  • Yes it is opened when i run the macro. I have my scripting enabled in SAP as well. – rob Jul 05 '19 at 02:18
  • Hi, i need to ask, is there a possibility that RZ11 caused all these problems? Because i dont have access to RZ11, thus i cant check the scripting options. – rob Jul 08 '19 at 01:51
  • I'm no expert in this space but if scripting isn't enabled and you don't have access to change it through RZ11 then yes, that would be a problem and the error thrown would likely be this or something similar. – Josh Jul 08 '19 at 04:29

2 Answers2

0

Error 91 is when the Object you are trying to use is set to Nothing. So although that line is throwing an error, there is nothing actually wrong with that line. What does need to be fixed is how we get the session object.

My preference for working with the session object is to setup a Function that takes care of that, then it is only a couple of lines of code in your main Sub/Function. One of our original functions that is not pretty but should do what you need goes like this...

    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

I would then start my macro by checking if there is a SAP session already set and if not find it before continuing with the rest of the program. Where one doesn't exist it will End the program.

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

I hope this helps and I hope this works. Let us know how you go.

Josh
  • 236
  • 1
  • 2
  • 12
0

But you could also try the following:

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

Regards, ScriptMan

ScriptMan
  • 1,580
  • 1
  • 9
  • 9
  • Doesnt work for me, error at Set session = connection.children(0) "The enumerator of the collection cannot find an element with specific index." – rob Jul 08 '19 at 01:43
  • It would be interesting if the changes above do something. – ScriptMan Jul 08 '19 at 07:11