4

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

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
dustinroepsch
  • 1,122
  • 1
  • 7
  • 24
  • Would the line `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 to `Connection.Children` – Paul Ogilvie Apr 20 '16 at 18:30
  • I have taken a screenshot of the debugger right before the line that error's out executes – dustinroepsch Apr 20 '16 at 18:37
  • So it looks like Children is empty, but I'm not sure why that would be. Am I opening SAP incorrectly? – dustinroepsch Apr 20 '16 at 18:37

5 Answers5

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

Marcel
  • 26
  • 3
0

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)

dustinroepsch
  • 1,122
  • 1
  • 7
  • 24
0

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!

Anonygreat
  • 11
  • 1
0

Please check if scripting is enabled. When you do client-level script enabling, it automatically reset if the server is restarted. You may face this problem on sandbox or demo system more often than production box :-)

gk bheli
  • 11
  • 3
0

Experienced this now. Started looking for the answer. It was because we restarted our SAP Server. Restarting the server resets the value of sapgui/user_scripting

You need to set sapgui/user_scripting to true every server reset.