cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

VB Script: check for existing open session

Former Member
0 Likes
14,736

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!

View Entire Topic
Stefan-Schnell
Active Contributor

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

Former Member

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?

Stefan-Schnell
Active Contributor

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