cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot retrieve session with VBA

MattS1
Newcomer
0 Kudos

Hi,

I am trying to retrieve my SAP session with the following VBA code

If Not IsObject(app) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set app = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = app.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject Application, "on"
End If

I get an error on the following line

Set session = Connection.Children(0)

Error message: The enumerator of the collection cannot find an element with the specified index.

User scripting is enabled on my machine. And I open SAP and login with my account before executing code above.

Can you please share your ideas on a resolution?

View Entire Topic
FrankKrauseGUI
Advisor
Advisor
0 Kudos

Hi,

apart from the question whether you have activated SAP GUI Scripting at all (see the other answers) I wonder whether you have a session at all at the time you run your script.

This command
Set session = Connection.Children(0)
can only work if you have opened a connection from SAP Logon.
SAP Logon itself does not have a scripting API - the API is basically only available for the main windows belonging to a connection and their children. Connection is the object which contains all available connections - if you have none, you will get the respective error.

If the later is the problem I advise that you first check the scripting API documentation.

Best regards,
Frank