Skip to Content

Hello community,

very often it is asked the question, how to select a specific session to do some SAP GUI Scripting activities inside it. The selection criterias are often the system ID (SID), a transaction code (TAC) or a program name. So I develop an example in VBScript resp. SAP GUI Scripting code, which offers these possibilities. It scans all connections with all sessions and select a specific session. The selection criterias in this example are properties of the session info object – here SID and TAC, but you can use what ever you want from the SAP GUI Scripting object hierarchy. If the SID is equal to NSP and the TAC is equal to SE80 the sub routine Action is called. In this sub routine you can store the part of your code from the SAP GUI Scripting recorder, which is below the object definition.

Enjoy this flexibility.

Cheers

Stefan

'-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.
'-
'- Author: Stefan Schnell
'-
'-----------------------------------------------------------------------

  '-Directives----------------------------------------------------------
    Option Explicit

  '-Sub Action----------------------------------------------------------
  '-
  '- Get the selected session and do the action inside it
  '-
  '---------------------------------------------------------------------
    Sub Action(session)

      '-Insert your SAP GUI Scripting code from recorder here-----------

        MsgBox session.findById("wnd[0]/titl").text
   
    End Sub

  '-Sub Main------------------------------------------------------------
  '-
  '- Main procedure to select the session
  '-
  '---------------------------------------------------------------------
    Sub Main()
   
      '-Local variables-------------------------------------------------
        Dim SapAppl, SapGuiAuto, CollCon, i, oCon, CollSes, j, oSes
        Dim oSesInf, SID, Trans

      Set SapGuiAuto = GetObject("SAPGUI")
      If Not IsObject(SapGuiAuto) Then
        Exit Sub
      End If

      Set SapAppl = SapGuiAuto.GetScriptingEngine
      If Not IsObject(SapAppl) Then
        Exit Sub
      End If

      Set CollCon = SapAppl.Connections()
      If Not IsObject(CollCon) Then
        Exit Sub
      End If
     
      '-Loop over connections-------------------------------------------
        For i = 0 To CollCon.Count() - 1

          Set oCon = SapAppl.Children(CLng(i))
          If Not IsObject(oCon) Then
            Exit Sub
          End If 

          Set CollSes = oCon.Sessions()
          If Not IsObject(CollSes) Then
            Exit Sub
          End If 
       
          '-Loop over sessions------------------------------------------
            For j = 0 To CollSes.Count() - 1
       
              Set oSes = oCon.Children(CLng(j))
              If Not IsObject(oSes) Then
                Exit Sub
              End If 

              If oSes.Busy() = vbFalse Then
         
                Set oSesInf = oSes.Info()
                If IsObject(oSesInf) Then

                  '-----------------------------------------------------
                  '-
                  '- With the session info object is it possible to
                  '- select a specific session which executes the
                  '- activities. In our example it is the system name
                  '- and the transaction code, but you can use all
                  '- properties of the session info object you want.
                  '-
                  '-----------------------------------------------------
                    SID = oSesInf.SystemName()
                    Trans = oSesInf.Transaction()

                    If SID = "NSP" And Trans = "SE80"  Then

                      Action oSes

                    End If

                End If
             
              End If

            Next

        Next

    End Sub

  '-Main----------------------------------------------------------------
    Main()
   
'-End-------------------------------------------------------------------
To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. JT McDonald

    Stefan,

    I’m hoping you can help with a problem that is somewhat similar. I’m trying to be able to have a pop up box that prompts you to select the session you want to use the script on. Once you click on the session then you will hit OK and SAP will then know the session you would like to run the script on.

    The code below works fine if I’m using a hotkey to launch the script.

    The problem comes when I’m trying to launch the script using HTA. I get an error saying “Object required: ‘session'”.

    How can I fix this? I need to be able to choose which sap session to run the script on while launching with HTA.

    It seems that running via HTA causes additional problems because the HTA window will steal focus from SAP.

    If Not IsObject(application) Then
       Set SapGuiAuto  = GetObject("SAPGUI")
       Set application = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(connection) Then
       Set connection = application.Children(0)
    End If
    msgbox "Please select a session to run the script on", vbSystemModal
    set session = application.ActiveSession

     

    (0) 
    1. Stefan Schnell Post author

      Hello JT,

      I check your problem and as you say, the HTA window gets the focus, because you open a message box in the context of the HTA window and your last activity is to press the button of this message box, so the message box is in top of the window z-order and therewith the HTA window.

      But you can try a tiny trick to solve your problem: Switch via SendKeys and Alt+Tab to the next window in the z-order, your SAP session, which is activated as the last before.

      set WshShell = WScript.CreateObject("WScript.Shell")
      WshShell.SendKeys "&{TAB}"

      At the moment I don’t have the possibility to check this solution, so please let us know your result.

      Cheers
      Stefan

      (0) 
      1. JT McDonald

        Stefan,

        I get the error Object Required: “WScript”. I did some googling and saw some results that said it won’t work that way using HTA and I need to remove the Wscript from before CreateObject. However, now I’m getting the error Object Required: “Session”. Below is the code that I last tried.

        If Not IsObject(application) Then
           Set SapGuiAuto  = GetObject("SAPGUI")
           Set application = SapGuiAuto.GetScriptingEngine
        End If
        If Not IsObject(connection) Then
           Set connection = application.Children(0)
        End If
        If Not IsObject(session) Then
           Set session    = connection.Children(0)
        End If
        If IsObject(WScript) Then
           WScript.ConnectObject session,     "on"
           WScript.ConnectObject application, "on"
        End If
        
        msgbox "Please select a session to run the script on", vbSystemModal
        
        set WshShell = CreateObject("WScript.Shell")
        WshShell.SendKeys "&{TAB}"
        
        set session = application.ActiveSession

         

         

        (0) 
        1. JT McDonald

           

          I also have tried a different approach and had some limited success. It would work for a couple of times to select the session that I clicked on, but then it would start defaulting back to the first session.

          Please see the code below.

          If Not IsObject(application) Then
             Set SapGuiAuto  = GetObject("SAPGUI")
             Set application = SapGuiAuto.GetScriptingEngine
          End If
          If Not IsObject(connection) Then
             Set connection = application.Children(0)
          End If
          If Not IsObject(session) Then
             Set session    = connection.Children(0)
          End If
          If IsObject(WScript) Then
             WScript.ConnectObject session,     "on"
             WScript.ConnectObject application, "on"
          End If
          
          msgbox "Please select a session to run the script on", vbSystemModal
          
          Set WScr = CreateObject("WScript.Shell") 
          
          sTitle = Application.Connections.Item(0).Sessions.Item(0).findById("wnd[0]").text
          
          WScr.AppActivate(""& sTitle &"")
          
          
          set session = application.ActiveSession
          (0) 
          1. Stefan Schnell Post author

            Hello JT,

            here a working solution with AutoItX, which is part from AutoIt scripting language, SendKeys from WScript.Shell doesn’t work.

            Your second solution doesn’t work, because you are using
            Connections.Item(0).Sessions.Item(0)
            which means Connection(0) and Session(0)
            and this isn’t what you want.

            Cheers
            Stefan

            <html>
            
              <head>
            
                <title>Test</title>
            
                <HTA:APPLICATION 
                  ID="Test" VERSION="0.01" APPLICATIONNAME="Test"
                  SYSMENU="yes" MAXIMIZEBUTTON="yes" MINIMIZEBUTTON="yes"
                  BORDER="thin" INNERBORDER="no" SCROLL="no" SINGLEINSTANCE="yes"
                  WINDOWSTATE="normal" />
            
              </head>
            
              <script language="VBScript">
            
                Sub Test()
            
                  If Not IsObject(application) Then
                    Set SapGuiAuto  = GetObject("SAPGUI")
                    Set app = SapGuiAuto.GetScriptingEngine
                  End If
            
                  If Not IsObject(connection) Then
                    Set connection = app.Children(0)
                  End If
            
                  Set oAutoIt = CreateObject("AutoItX3.Control")
                  If IsObject(oAutoIt) Then
            
                    MsgBox "Please select a session to run the script on", vbSystemModal
            
                    oAutoIt.Send "!{TAB}"
            
                    Set session = app.ActiveSession
            
                    MsgBox session.Info.Client
            
                  End If
            
                End Sub
            
              </script>
            
              <body bgcolor="white">
                <input type="button" name="btnStart" id="btnStart" value="Start" onclick="Test">
              </body>
            
            </html>

             

            (0) 

Leave a Reply