Maybe someone can help me with creating new SAP GUI session using VBA Excel.
Some code to understand the problem:
If Not IsObject(sap) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set sap = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = sap.Connections.Item(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
In most cases this one works fine. But sometimes this part doesn't work:
Set session = Connection.Children(0)
it happens for example when SAP GUI timeout occurs (auto logoff after some idle time). In that case I have:
sap.Connections.Count = 2
but
Connection.Sessions.Count = 0
Looks like the timeouted connection still hangs somewhere in SAP GUI. So when I try to connect to first session of first connection I got an error because there is no session in first connection.
What I want to do is to create new session?
I can do this by
Dim sapSession As SAPFEWSELib.GuiSession
Dim sapCon As SAPFEWSELib.GuiConnection
Set sapCon = sap.Connections.Item(0)
Set sapSession = Connection.sessions.Item(0)
sapsession.createsession
This one works fine but it doesn't help because I still need to set the session first.
Is there a way to create session after setting the connection? Something like sapCon.createsession
?
And does anyone know how can I use specific session using variable?
Set sapSession = Connection.sessions.Item(0)
This works fine but when I try
Dim SessionNumber as integer
....
SessionNumber = 0
Set sapSession = Connection.sessions.Item(SessionNumber)
it throws an error:
Bad index type for collection access
Excel requires that the session number is an integer, so you can use the type conversion from Cint(). Strange that this is advised/required even when SessionNumber is defined as an integer.
Dim SessionNumber
....
SessionNumber = 0
Set sapSession = Connection.sessions.Item(Cint(SessionNumber))