on 2014 Feb 05 2:09 PM
I use the code below often. It is part of many automated processes I call from some function modules in Access as a step in a macro. As the code is written now, it kills any existing connection or session and logs me in and runs whatever procedures I have. The problem with this is I also work in SAP throughout the day and my automated script kicks me out periodically. I can have up to 5 sessions open at once. I have no clue how to make the script below do the following:
1. First, check if an open session exists.
2. If a session exists, open a new one.
3. If 5 open sessions exist, pause and generate a message box alerting me that "5 sessions already exist"
Function GrabOrdersToday()
Dim Application As Variant
Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
Set Connection = Application.OpenConnection("PRD")
Set SapSession = Connection.Children(0)
If IsObject(WScript) Then
WScript.ConnectObject SapSession, "on"
WScript.ConnectObject Application, "on"
End If
SapSession.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "myuserid"
SapSession.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "mypassword"
SapSession.findById("wnd[0]/usr/pwdRSYST-BCODE").SetFocus
SapSession.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8
SapSession.findById("wnd[0]").sendVKey 0
'In case you are already logged in...
If SapSession.Children.Count > 1 Then
SapSession.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").Select
SapSession.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").SetFocus
SapSession.findById("wnd[1]/tbar[0]/btn[0]").press
End If
...and then my actual script starts here where I run transactions or whatever I need. I picked up this code a long time ago and it looks like it has some sort of If statement that is supposed to check for a login, but it doesn't. Every time this code runs, it kills the existing session and logs me into SAP all over again. I only want it to log me into SAP if no current session exists. Thanks in advance for any help!
Help others by sharing your knowledge.
AnswerRequest clarification before answering.
Sub Get_all_SAP_Sessions()
Dim SapGuiAuto As Object
Dim i%
Dim iSession%
Dim sapapplication
Dim Connection As SAPFEWSELib.GuiConnection
Dim Session As SAPFEWSELib.GuiSession
Dim strSessions$
i = 1
iSession = 0
' There may be bad entries in the ROT from previous crashes
While i < 10 And SapGuiAuto Is Nothing
i = i + 1
On Error Resume Next
Set SapGuiAuto = GetObject("SAPGUI")
On Error GoTo 0
Wend
If SapGuiAuto Is Nothing Then
MsgBox "Could not connect to SAPlogon process. Did you start it?"
Exit Sub
End If
On Error Resume Next
Set sapapplication = SapGuiAuto.GetScriptingEngine
Set SapGuiAuto = Nothing
On Error GoTo 0
If sapapplication Is Nothing Then
MsgBox "Could not access GuiApplication. Maybe Scripting is disabled?"
Exit Sub
End If
Set SapGuiAuto = Nothing
For Each Connection In sapapplication.Children
If Not Connection.DisabledByServer Then
For Each Session In Connection.Children
If Session.Busy = False Then
iSession = iSession + 1
strSessions = strSessions & iSession & " => " & (Session.Info.SystemName & _
" (" & CStr(Session.Info.SessionNumber) & _
") (" & Session.Info.Client & ") | User: " & _
Session.Info.User & " | Transaction: " & _
Session.Info.Transaction) & vbCrLf & iSession & " => System-ID: " & Session.ID & vbCrLf
End If
Next
End If
Next
MsgBox strSessions
End Sub
Above an example how I achieve to get all available SAP Sessions. Additional I implement an check if a Session is busy. In this case it will skip to get session info as this will block the check macro.
Holger
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.