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

12,679

Solution 1

I think something else I installed (or maybe even my company's updating scripts), messed up the vba support that is installed when you install SAP.

Re installing the SAP Logon application fixed my issue

(if someone else gives a better reason to WHY this happened, I'll accept that answer)

Solution 2

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

Share:
12,679
dustinroepsch
Author by

dustinroepsch

Updated on June 26, 2022

Comments

  • dustinroepsch
    dustinroepsch almost 2 years

    A few weeks ago I generated a script via SAP's built in 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 en 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 similiar problem, and know how I can successfully open an SAP 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