excelvbasap-gui

SAP GUI Scripting Error, "The Enumerator of the collection cannot find an element with the specified index."


A few weeks ago I generated a script via SAP GUI Scripting functionality, and then put the VBA in an Excel document and attached it to a button.

This worked perfectly fine for few weeks, however now when I press the button, I get this error:

The enumerator of the collection cannot find an element with the specified index. on the line that says Set session = Connection.Children(0). Here is a snippet of code:

If Not IsObject(Sap_Application) Then
    Set SapGuiAuto = GetObject("SAPGUI")
    Set Sap_Application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
    Set Connection = Sap_Application.Children(0)
End If
If Not IsObject(session) Then
    Set session = Connection.Children(0) 'This is the line that causes the error
End If
If IsObject(WScript) Then
    WScript.ConnectObject session, "on"
    WScript.ConnectObject Sap_Application, "on"
End If

I have SAP open and I'm logged in, and all of the code in the snippet was generated by SAP GUI scripting.

I tried a few solutions I saw online (like adding some OCX files that were in my sap install folder into my Excel addons) but nothing seems to be working.

I was wondering if anyone on here has ran into a similar problem, and know how I can successfully open an SAP GUI session in my VBA.

EDIT FROM COMMENTS:

When I inspect the connection object, this is what I see. It looks like Children length is 0.

Debugger View


Solution

  • Had the same problem and found this thread, so I'm reviving it with the solution that worked for me:

    Scripting is probably deativated. sapgui/user_scripting must be set to true