Hello Sayuti,
here an VBS example how to check the existence of a connection or a session.
'-Begin--------------------------------------------------------------
'-Function ConnectionExists----------------------------------------
Function ConnectionExists(oConnection)
'-Variables----------------------------------------------------
Dim SAP, SAPGUI, Connections, cntConnection, i, Connection
Set SAP = GetObject("SAPGUI")
If Not IsObject(SAP) Then
Exit Function
End If
Set SAPGUI = SAP.GetScriptingEngine()
If Not IsObject(SAPGUI) Then
Exit Function
End If
Set Connections = SAPGUI.Connections()
If Not IsObject(Connections) Then
Exit Function
End If
cntConnection = Connections.Count()
'-Here a loop on the connections to find the correct-----------
For i = 0 To cntConnection - 1
Set Connection = SAPGUI.Connections(CLng(i))
If IsObject(Connection) Then
'-Here the check with the ID and the name----------------
If Connection.Id = oConnection.Id And _
Connection.Description = oConnection.Description Then
ConnectionExists = vbTrue
Exit Function
End If
End If
Next
ConnectionExists = vbFalse
End Function
'-Function SessionExists-------------------------------------------
Function SessionExists(oSession)
'-Variables----------------------------------------------------
Dim SAP, SAPGUI, Connections, cntConnection, i, Connection
Dim Sessions, cntSession, j, Session
Set SAP = GetObject("SAPGUI")
If Not IsObject(SAP) Then
Exit Function
End If
Set SAPGUI = SAP.GetScriptingEngine()
If Not IsObject(SAPGUI) Then
Exit Function
End If
Set Connections = SAPGUI.Connections()
If Not IsObject(Connections) Then
Exit Function
End If
cntConnection = Connections.Count()
'-Here the loop on the connections-----------------------------
For i = 0 To cntConnection - 1
Set Connection = SAPGUI.Connections(CLng(i))
If IsObject(Connection) Then
Set Sessions = Connection.Sessions()
If IsObject(Sessions) Then
cntSession = Sessions.Count()
'-Here the loop on the sessions------------------------
For j = 0 To cntSession - 1
Set Session = Connection.Sessions(CLng(j))
If IsObject(Session) Then
'-Here the check with the ID and the name--------
If Session.Id = oSession.Id And _
Session.Name = oSession.Name Then
SessionExists = vbTrue
Exit Function
End If
End If
Next
End If
End If
Next
SessionExists = vbFalse
End Function
'-Sub TestConnection (only for test, not really important)---------
Sub TestConnection()
'-Variables----------------------------------------------------
Dim SAP, SAPGUI, Connection
Set SAP = GetObject("SAPGUI")
If SAP Is Nothing Then
Exit Sub
End If
Set SAPGUI = SAP.GetScriptingEngine()
If SAPGUI Is Nothing Then
Exit Sub
End If
Set Connection = SAPGUI.Connections(0)
If ConnectionExists(Connection) Then
MsgBox "Exists"
Else
MsgBox "Don't exists"
End If
End Sub
'-Sub TestSession (only for test, not really important)------------
Sub TestSession()
'-Variables----------------------------------------------------
Dim SAP, SAPGUI, Connection, Session
Set SAP = GetObject("SAPGUI")
If SAP Is Nothing Then
Exit Sub
End If
Set SAPGUI = SAP.GetScriptingEngine()
If SAPGUI Is Nothing Then
Exit Sub
End If
Set Connection = SAPGUI.Connections(0)
Set Session = Connection.Sessions(1)
If SessionExists(Session) Then
MsgBox "Exists"
Else
MsgBox "Don't exists"
End If
End Sub
'-Main-------------------------------------------------------------
TestConnection
TestSession
'-End----------------------------------------------------------------
Hope it is easy enough to understand.
If you have questions, you are welcome.
Cheers
Stefan