Skip to Content
Mar 21, 2019 at 05:29 AM

Trouble Connecting to SAP using VBA

556 Views Last edit Mar 20, 2019 at 02:32 PM 3 rev


I am trying to open a connection in Sap using a macro in vba. The macro opens a new connection, but runs into the error "The enumerator of the collection cannot find en element with the specified index" while processing....

Set session = Connection.Children(0)

I have looked everywhere for people encountering the same error, and have not found a solution. This is basically my last resort. My code is posted below....

My ultimate goal is to use session.findbyId to extract commands from the SAP statusbar, but I am failing to properly define session...

'----------- Macro

Sub Logontrial()

Dim SAPGUIAuto As Object
Dim App As SAPFEWSELib.GuiApplication
Dim Connection As SAPFEWSELib.GuiConnection
Dim session As SAPFEWSELib.GuiSession

Set session = Nothing
Set Connection = Nothing

Set SAPGUIAuto = GetObject("SAPGUI")
Set App = SAPGUIAuto.GetScriptingEngine

Application.Wait (Now + TimeValue("00:00:05"))

Set Connection = App.OpenConnection("P81: Produktion GS")
Application.Wait (Now + TimeValue("00:00:05"))

Set Connection = App.Children(0)

Set session = Connection.Children(0)

End Sub

This is a screenshot of my watched when in debugging...


akxrv.png (58.8 kB)