Skip to Content
avatar image
Former Member

VBA detect SAP user

Hello!

The thing goes that I use more than one user for SAP, each can access diffetent T-codes. Therefore I would need the vba code to detect if a specific user has an open session, if not to open a new session for it.

As of now here it is what I could write. This code would detect if there is an open SAP session and if not it will open a new one. What I am missing is to detect if a user (let say GEN1) has an open session and to work on that window.

Set SapGuiAuto = GetObject("SAPGUI")

Set SAPApp = SapGuiAuto.GetScriptingEngine

If SAPApp.Connections.Count() = 0 Then

Call Shell("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus)

waitTill = Now() + TimeValue("00:00:05")

While Now() < waitTill

DoEvents

Wend

Set SapGuiAuto = GetObject("SAPGUI")

Set SAPApp = SapGuiAuto.GetScriptingEngine

Set Connection = SAPApp.openconnection("PRD")

Set session = Connection.Children(0)

session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "Mandant"

session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "User"

session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "PW"

session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN"

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

PS: all Users work in the same SAP connection = PRD

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    May 17 at 05:41 AM

    Hello Julian,

    welcome in the SAP Community.

    Here an example how to detect a session with the SID and the user:

    '-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 based on system ID (SID) and
    '- transaction code (TAC).
    '-
    '-----------------------------------------------------------------------
    
      '-Directives----------------------------------------------------------
      Option Explicit
    
      '-Function FindSession------------------------------------------------
      '-
      '- Find a specific SAP GUI session
      '-
      '---------------------------------------------------------------------
      Function FindSession(SID, USER)
    
        '-Local variables-------------------------------------------------
        Dim SapAppl, SapGuiAuto, CollCon, i, oCon, CollSes, j, oSes
        Dim oSesInf
    
        Set SapGuiAuto = GetObject("SAPGUI")
        If Not IsObject(SapGuiAuto) Then
          Exit Function
        End If
    
        Set SapAppl = SapGuiAuto.GetScriptingEngine
        If Not IsObject(SapAppl) Then
          Exit Function
        End If
    
        Set CollCon = SapAppl.Connections()
        If Not IsObject(CollCon) Then
          Exit Function
        End If
       
        '-Loop over connections-------------------------------------------
        For i = 0 To CollCon.Count() - 1
    
          Set oCon = SapAppl.Children(CLng(i))
          If Not IsObject(oCon) Then
            Exit Function
          End If 
    
          Set CollSes = oCon.Sessions()
          If Not IsObject(CollSes) Then
            Exit Function
          End If 
        
          '-Loop over sessions--------------------------------------------
          For j = 0 To CollSes.Count() - 1
        
            Set oSes = oCon.Children(CLng(j))
            If Not IsObject(oSes) Then
              Exit Function
            End If 
    
            If oSes.Busy() = vbFalse Then
              Set oSesInf = oSes.Info()
              If IsObject(oSesInf) Then
                If oSesInf.SystemName() = SID And _
                  oSesInf.User() = USER  Then
                  Set FindSession = oSes
                  Exit Function
                End If
              End If
            End If
    
          Next
    
        Next
    
        Set FindSession = Nothing
      
      End Function
    
      '-Sub Main------------------------------------------------------------
      Sub Main()
    
        '-Local variables-------------------------------------------------
        Dim oSes
    
        Set oSes = FindSession("PRD", "GEN1")
    
        If TypeName(oSes) <> "Nothing" Then
    
          '-Insert your SAP GUI Scripting code from recorder here-----------
          MsgBox oSes.findById("wnd[0]/titl").text
    
        End If
    
      End Sub
    
      '-Main----------------------------------------------------------------
      Main()
       
    '-End-------------------------------------------------------------------
    
    

    The function FindSession delivers the session with the given SID and user name.

    Let us know your results.

    Cheers
    Stefan

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 18 at 05:16 AM

    Hi Stefan,

    Thank you so much for the help! it is really helpul and a powerfull code!

    I have 2 further questions that pop-up whilñe analysing it.

    First, why have you used a Function instead of another Sub? I mean, what are the advantages of using Functions?

    On the other hand, I may need to go from SAP to Excel various times and form your code I got the idea that I should run it every time I would need to go back to SAP. So, is there a way to save in a variable the # of SAP window to use and then use that variable to specify which connection to use evey time?

    Thanks in advance!

    Regards,

    Jules

    Add comment
    10|10000 characters needed characters exceeded

    • Hello Julian,

      to your questions:

      1. The difference between a Sub and a Function is, that the Function delivers a return value. With a Sub routine it is not possible to do that.
      2. Sure can you do that. It is in this case only necessary to ensure that the connection respectively the session still exists if you want to use it. On nearly the same way as my example detects a session you can detect a connection and use it on the way you described.

      Best regards
      Stefan

  • avatar image
    Former Member
    May 18 at 05:16 AM

    Excelent code Stefan! Extremelly helpful and powerful! ;)

    Add comment
    10|10000 characters needed characters exceeded