Skip to Content
Former Member
Feb 05, 2014 at 02:09 PM

VB Script: check for existing open session


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").caretPosition = 8

SapSession.findById("wnd[0]").sendVKey 0

'In case you are already logged in...

If SapSession.Children.Count > 1 Then




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!