Skip to Content
avatar image
Former Member

How to find a session without a transaction and link it to SAP

Experts,

i am new to the world of scripting. I have gone through the posts about attaching new sessions.

My goal is to scroll through all the open sessions; find the one which is available; if a session is available (free, no Transaction) then link it with VBA otherwise create a new session.

I have put together this code but somehow the createsession is not being executed. Also the system attaches all the sessions. Can you kindly look into this code and tweak it for the desired output.

Thanks

Amit

---------------------------------------------------------------------------------

Public Sub cmdattachx_Click() ' find and attach to an active SAP session

On Error GoTo Err_AttachConnSess

Dim session_nr As Integer

session_nr = -1

'Get the application object from the Running Object Table

Set GuiAuto = GetObject("SAPGUI")

Set sapapplication = GuiAuto.GetScriptingEngine

'Get the first session of the first connection

Set Connection = sapapplication.Children(0)

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

             'MsgBox strsessions

             If session.info.Transaction = "SESSION_MANAGER" Then

                 session_nr = session.info.sessionnumber - 1

                 MsgBox session_nr

                 Set session = Connection.Children(Int(session_nr))

                 session.TestToolMode = 1

                 Exit Sub

              End If

         End If

     Next

End If

Next

If session_nr = -1 Then

     session.createsession

End If

Exit Sub

Err_AttachConnSess:

Err.Clear

'tmp = Err.Description

'msg = "oops, no active SAP session found. Please have two SAP Screen Open."

'MsgBox tmp

'Module1.cmdlogon 'redirect to logon procedure

Resume

End Sub

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Jan 05, 2016 at 04:07 AM

    Hello Amit,

    the code looks good, here my modifications. It is necessary to set the session variable to get the first free session and then to create a new one. In your case the seesion variable is not set.

    Sub cmdattachx()

      session_nr = -1

      Set GuiAuto = GetObject("SAPGUI")

      Set sapapplication = GuiAuto.GetScriptingEngine

      For Each Connection In sapapplication.Children

        If Not Connection.DisabledByServer Then

          For Each session In Connection.Children

            If session.Busy = False Then

              If session.info.Transaction = "SESSION_MANAGER" Then

                session_nr = session.info.sessionnumber - 1

                Set session = Connection.Children(Int(session_nr))

                session.TestToolMode = 1

                Exit Sub

              End If

            End If

          Next

        End If

      Next

      If session_nr = -1 Then

        '-Searching for the first free session and create a new one---------

        For Each Connection In sapapplication.Children

          If Not Connection.DisabledByServer Then

            For Each session In Connection.Children

              If session.Busy = False Then

                session.createsession

              End If

            Next

          End If

        Next

      End If

    End Sub

    Let us know your results.

    Cheers

    Stefan

    Add comment
    10|10000 characters needed characters exceeded

    • Hello Amit,

      please disable the line

      Set session = Connection.Children(Int(session_nr))

      it is not necessary. With the code

      For Each session In Connection.Children

      you have already the session object and it is not necessary to set it again.

      Let us know your results.

      Cheers

      Stefan