cancel
Showing results for 
Search instead for 
Did you mean: 

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

0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor
0 Kudos

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

0 Kudos

Hello Stefan,

Thanks for the response. I have tried your code which works and creates a new session. But I still have some minor issues with the flow.

1. If i have 3 sessions open and i execute the red marked code, then the system connects all the open sessions. In the end i have then 3 different sessions connected.

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

2. Further if one of these three sessions is not being used, then the system tries to connect to this open session (blue code) and this triggers an error.

The requirement is following.

1. Scan all the open sessions

2. Find the session without a transaction associated

3. If a session is found then connect this session and activate it. All other sessions are not connected anymore.

4. If no session is found, then create a session, activate it and use it for the new transaction.

Thanks for your help,

Amit

stefan_schnell
Active Contributor
0 Kudos

Hello Amit,

please try this to solve your requirement:

Private Declare Function BringWindowToTop Lib "user32" (ByVal _

  hwnd As Long) As Long


Function cmdattachx() As Object

  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))

            hwnd = session.ActiveWindow.Handle

            BringWindowToTop hwnd

            session.ActiveWindow.SetFocus

            session.TestToolMode = 1

            Set cmdattachx = session

            Exit Function

          End If

        End If

      Next

    End If

  Next

  If session_nr = -1 Then

    For Each Connection In sapapplication.Children

      If Not Connection.DisabledByServer Then

        For Each session In Connection.Children

          If session.Busy = False Then

            session.createsession

            sessionFound = True

            Exit For

          End If

        Next

        If sessionFound = True Then

          Exit For

        End If

      End If

    Next

  End If

End Function


Sub Test()

  Set session = cmdattachx()

  If session Is Nothing Then

    Application.Wait (Now + TimeValue("0:00:03"))

    Set session = cmdattachx()

  End If

End Sub

Let us know your results.

Cheers

Stefan

0 Kudos

Stefan,

this worked like a charm without any errors. I have extended this code to only check if the maximum # of sessions are already open. Here is my final code for other users


thanks


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

Function cmdattachx() As Object

  Session_Nr = -1

  Set GuiAuto = GetObject("SAPGUI")

  Set sapapplication = GuiAuto.GetScriptingEngine

  For Each Connection In sapapplication.Children

    If Not Connection.DisabledByServer Then

    session_number_all = Connection.Children.Count

      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))

            hwnd = session.ActiveWindow.Handle

            BringWindowToTop hwnd

            session.ActiveWindow.SetFocus

            session.TestToolMode = 1

            Set cmdattachx = session

            Exit Function

          End If

        End If

      Next

    End If

  Next

 

    If session_number_all = 6 And Session_Nr = -1 Then

        MsgBox "Maximum number of sessions reached"

        Exit Function

    End If

  If Session_Nr = -1 Then

    For Each Connection In sapapplication.Children

      If Not Connection.DisabledByServer Then

        For Each session In Connection.Children

          If session.Busy = False Then

            session.createsession

            sessionFound = True

            Exit For

          End If

        Next

        If sessionFound = True Then

          Exit For

        End If

      End If

    Next

  End If

End Function

Sub Test()

  Set session = cmdattachx()

  If session Is Nothing Then

    Application.Wait (Now + TimeValue("0:00:03"))

    Set session = cmdattachx()

  End If

End Sub

stefan_schnell
Active Contributor
0 Kudos

Hello Amit,

thanks for your reply and your code.

Cheers

Stefan

0 Kudos

Stefan

i just realised that the code is not dynamic. If i have 3 sessions open and I close the session # 1 or 2, then the code tries to find the session 1 or 2 and stops there.

In fact, the code should reflect on the sessions available at a given point of time. Any ideas how we could make it more dynamic.

Thanks

Amit

stefan_schnell
Active Contributor
0 Kudos

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

Answers (0)