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.
Hi Jason,
welcome in the Scripting Language forum.
Here a VBA snippet to connect an SAP System, if no other connections to the system exists:
Function GrabOrdersToday()
Dim SapGuiAuto As Object
Dim Application As SAPFEWSELib.GuiApplication
Dim Connection As SAPFEWSELib.GuiConnection
Dim i As Integer
Dim Conn As String
Dim Flag As Boolean
Flag = False
Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
If Application.Connections.Count() > 0 Then
For i = 0 To Application.Connections.Count() - 1
Set Connection = Application.Children(i)
Conn = Connection.Description()
If Conn = "PRD" Then
Flag = True
Exit For
End If
Next
If Flag = False Then
Set Connection = Application.OpenConnection("PRD")
End If
'Insert your code here
Set Connection = Nothing
Else
Set Connection = Application.OpenConnection("PRD")
'Insert your code here
Set Connection = Nothing
End If
End Function
Cheers
Stefan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you for your reply! This looks very promising. I am now getting an error "User-Defined Type not defined" at this line:
Dim Application As SAPFEWSELib.GuiApplication
I assume I am missing a Reference in my database for that object library? It's not available in my current list of libraries as SAPFEWSELib. Could it be called something else? Or am I on the wrong path here?
Hi Jason,
add in your VBA project a reference to the SAP GUI Scripting library.
Choose the menu Tools > References and press the button Browse... Now choose the file C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.ocx. This binds the SAP GUI Scripting API into the VBA-IDE. Press the Ok button and now you can use the SAPFEWSELib namespace.
This should solve your problem.
Good luck.
Cheers
Stefan
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.