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.
Set Connection = Sap_Application.Children(0)
throw an error if the connection cannot be made? You can use the debugger, Watches window to inspect the object that you get. You should be able to click down toConnection.Children
– Paul Ogilvie