Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
stefan_schnell
Active Contributor
41,861
Very often it is asked the question, how to select a specific session to do some SAP GUI Scripting activities inside it. The selection criterias are often the system ID (SID), a transaction code (TAC) or a program name. So I develop an example in VBScript resp. SAP GUI Scripting code, which offers these possibilities. It scans all connections with all sessions and select a specific session. The selection criterias in this example are properties of the session info object - here SID and TAC, but you can use what ever you want from the SAP GUI Scripting object hierarchy. If the SID is equal to NSP and the TAC is equal to SE80 the sub routine Action is called. In this sub routine you can store the part of your code from the SAP GUI Scripting recorder, which is below the object definition.
'-Begin-----------------------------------------------------------------
'-
'- Example to show how to select a specific session to do SAP GUI
'- Scripting activities inside it. It scans all connections with all
'- sessions to find the correct one.
'-
'- Author: Stefan Schnell
'-
'-----------------------------------------------------------------------


'-Directives------------------------------------------------------------
Option Explicit


'-Sub Action------------------------------------------------------------
'-
'- Get the selected session and do the action inside it
'-
'-----------------------------------------------------------------------
Sub Action(session)

'Insert your SAP GUI Scripting code from recorder here

MsgBox session.findById("wnd[0]/titl").text

End Sub


'-Function GetSession---------------------------------------------------
'-
'- Detects the session
'-
'-----------------------------------------------------------------------
Function GetSession(SID, TAC)

Dim SapGuiAuto, application, connections, connection, sessions
Dim session, sessionInfo, j, i

Set SapGuiAuto = GetObject("SAPGUI")
If Not IsObject(SapGuiAuto) Then
Exit Function
End If

Set application = SapGuiAuto.GetScriptingEngine
If Not IsObject(application) Then
Set SapGuiAuto = Nothing
Exit Function
End If

Set connections = application.Connections()
If Not IsObject(connections) Then
Set SapGuiAuto = Nothing
Set application = Nothing
Exit Function
End If

'-Loop over connections-----------------------------------------------
For Each connection In connections
Set sessions = connection.Sessions()
'-Loop over sessions------------------------------------------------
For Each session In sessions
If session.Busy() = vbFalse Then
'---------------------------------------------------------------
'-
'- With the session info object is it possible to select a
'- specific session which executes the activities. In our
'- example it is the system name and the transaction code, but
'- you can use all properties of the session info object.
'-
'---------------------------------------------------------------
Set sessionInfo = session.Info()
If sessionInfo.SystemName() = SID And _
sessionInfo.Transaction() = TAC Then
Set GetSession = session
End If
End If
Next
Next

End Function

'-Sub Main--------------------------------------------------------------
'-
'- Main procedure to select the session
'-
'-----------------------------------------------------------------------
Sub Main()

Dim session

Set session = GetSession("NSP", "SE80")
Action session

End Sub

'-Main------------------------------------------------------------------
Main()

'-End-------------------------------------------------------------------
33 Comments
Labels in this area