问题
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.
回答1:
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
回答2:
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)
回答3:
I know it's old, but probably might help someone else. I don't know the reason for this, out of the blue started having problems with my scripts. I recorded a new operation to see what SAP would write for those actions (since that part of the code is standard for the connection). Same code, same problem.
Fix (or at least temporary): Change the number of the child being searched. Previous code:
*Set Connection = Sap_Application.Children(0)*
New code:
*Set Connection = Sap_Application.Children(1)*
Not sure what the difference is, I know that if you also change the number in this line:
*Set session = Connection.Children(0) 'This is the line that causes the error*
the execution takes place in the second session, if you do change this one, also have to change the previous line of code.
Hope it helps!
来源:https://stackoverflow.com/questions/36751819/sap-gui-scripting-error-the-enumerator-of-the-collection-cannot-find-an-elemen