How to deal with multiple SAP logons?

We often run a macro that connects to SAP, being already logged in it. In this article I’m going to show You how to deal with multiple SAP logons.

How to deal with multiple SAP logons wizard with log

If You are using SAP on the daily basis, multiple logon happens. I know that from my experience. The pop up window appears, which nobody was expecting, including the code.

How to deal with multiple SAP logons pop up window

With this window probably comes along an error, which said that no control could be found with given ID.

How to deal with multiple SAP logons vbe error

So, what are the options?

You can:
1. Continue this session, end others.
2. Continue this session, don’t end others, but some serious stuff is going to be investigated by SAP.
3. Terminate this logon.

In case You are sure that everything You did previously is saved, You can freely choose 1st option. If not choose 3rd option, save everything You’ve done and try to launch macro again.

And to be honest, I don’t really feel the second option, so I won’t talk about it. I just don’t recommend this solution.

So, how could You prevent your macros from this situation?

There are few possible approaches to this case and I’m going to show You my. The example codes implement right after the login and password confirmation.

 session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "LOGIN"
 session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "PASSWORD"
 session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN"

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

Count the number of sessions

This example I already showed in parent article – Connect to SAP via Excel VBA, but not really described.

If session.Children.Count > 1 Then

    MsgBox "You are already logged in SAP", vbOKOnly, _
        "Multiple SAP logons"

    session.findById("wnd[1]/usr/radMULTI_LOGON_OPT3").Select
    session.findById("wnd[1]/tbar[0]/btn[0]").press
            
    Exit Sub

End If

This code checks if there is more than 1 SAP session already on. If yes it pops out the message box and selecting the 3rd option – terminate this session.

Check if pop up window exists

In this example I set additional object for pop up window. If it exists, question appears . Based on answer macro is continuing to work or closing that session and exit the macro.

On Error Resume Next
    Dim wnd1 As Object
    Set wnd1 = session.findById("wnd[1]")
On Error GoTo 0

If Not (wnd1 Is Nothing) Then

    Dim answer As Integer
    answer = MsgBox("Continue?", vbQuestion + vbYesNo + vbDefaultButton2, _
        "Multiple SAP logons")
    
    If answer = vbYes Then
        session.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").Select
        session.findById("wnd[1]/tbar[0]/btn[0]").press
    Else
        session.findById("wnd[1]/usr/radMULTI_LOGON_OPT3").Select
        session.findById("wnd[1]/tbar[0]/btn[0]").press
        Exit Sub
    End If
End If

Simple error handling

Instead of checking the session counter or if pop up window exists You can simply put execution code in error brackets.

On Error Resume Next
    session.findById("wnd[1]/usr/radMULTI_LOGON_OPT3").Select
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    If Err.Number = 0 Then
        Exit Sub
    End If
On Error GoTo 0

If the pop up SAP window appears, termination code line will work. This will give no error, so code will end.

In case there is no additional window, the code will just go through the lines thanks to error handling brackets. Code will not end, because this will give error and conditional function won’t work this time.

Now You can handle this by yourself!

Now You will have no problem to deal with multiple SAP logons pop up window. Find the most convenient solution, which fits your style of coding the best. Modify it for your needs and have fully automated SAP macros.

Tomasz Płociński

Author: Tomasz Płociński

Excel VBA enthusiast who is also open for other languages. Mainly working in VBA, some SQL, hungry for more.

7 thoughts on “How to deal with multiple SAP logons?”

  1. Hello,
    thank you for the article.
    Just one question:

    Where should I enter code below “Check if pop up window exists”, after which part of main code?
    On Error Resume Next
    Dim wnd1 As Object
    Set wnd1 = session.findById("wnd[1]")
    On Error GoTo 0

    If Not (wnd1 Is Nothing) Then
    Dim answer As Integer
    answer = MsgBox("Continue?", vbQuestion + vbYesNo + vbDefaultButton2, _
    "Multiple SAP logons")


    If answer = vbYes Then
    session.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").Select
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    Else
    session.findById("wnd[1]/usr/radMULTI_LOGON_OPT3").Select
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    Exit Sub
    End If
    End If

    1. After the login and password confirmation:
      session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "900"
      session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "user"
      session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "password"
      session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN"

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

  2. is it coorect full code?

    Sub SapConn()

    Dim Appl As Object
    Dim Connection As Object
    Dim session As Object
    Dim WshShell As Object
    Dim SapGui As Object

    ‘SAP file directory
    Shell “C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe”, 4
    Set WshShell = CreateObject(“WScript.Shell”)

    Do Until WshShell.AppActivate(“SAP Logon “)
    Application.Wait Now + TimeValue(“0:00:01”)
    Loop

    Set WshShell = Nothing

    Set SapGui = GetObject(“SAPGUI”)
    Set Appl = SapGui.GetScriptingEngine

    ‘SAP system
    Set Connection = Appl.Openconnection(“Productive System”, True)
    Set session = Connection.Children(0)

    ‘ username, password and language
    session.findById(“wnd[0]/usr/txtRSYST-MANDT”).Text = “100”
    session.findById(“wnd[0]/usr/txtRSYST-BNAME”).Text = “User”
    ‘session.findById(“wnd[0]/usr/pwdRSYST-BCODE”).Text = “password”
    session.findById(“wnd[0]/usr/txtRSYST-LANGU”).Text = “EN”

    ‘multiple SAP logons
    On Error Resume Next
    Dim wnd1 As Object
    Set wnd1 = session.findById(“wnd[1]”)
    On Error GoTo 0

    If Not (wnd1 Is Nothing) Then

    Dim answer As Integer
    answer = MsgBox(“Continue?”, vbQuestion + vbYesNo + vbDefaultButton2, _
    “Multiple SAP logons”)

    If answer = vbYes Then
    session.findById(“wnd[1]/usr/radMULTI_LOGON_OPT1”).Select
    session.findById(“wnd[1]/tbar[0]/btn[0]”).press
    Else
    session.findById(“wnd[1]/usr/radMULTI_LOGON_OPT3”).Select
    session.findById(“wnd[1]/tbar[0]/btn[0]”).press
    Exit Sub
    End If
    End If
    ‘end multiple SAP logons

    If session.Children.Count > 1 Then

    answer = MsgBox(“You’ve got opened SAP already,” & _
    “please leave and try again”, vbOKOnly, “Opened SAP”)

    session.findById(“wnd[1]/usr/radMULTI_LOGON_OPT3”).Select
    session.findById(“wnd[1]/usr/radMULTI_LOGON_OPT3”).SetFocus
    session.findById(“wnd[1]/tbar[0]/btn[0]”).press

    Exit Sub

    End If

    session.findById(“wnd[0]”).maximize
    session.findById(“wnd[0]”).sendVKey 0

    End Sub

    1. First of all, did You check it? Did You launch it?
      Secondly, if You use:
      If Not (wnd1 Is Nothing) Then
      ...
      End If

      You don’t need:
      If session.Children.Count > 1 Then
      ...
      End If

  3. Hello Tomasz,
    Yes it is working

    When I run script first time the script is open SAP and performs respective actions in SAP – it’s ok.
    When I run Script second time I have message box with question if I would like continue session or terminate it.
    If I choose “yes” Script close all my SAP windows and create a new one and start perfom actions.

    Is there any possibilities to modify code that if I have already opened SAP the Script will create new window in current sission?

    So, if I run script first time and SAP is not opened – script open it and perform actions. As it’s currently.
    If SAP already open – script will ask me “Would you like to continue perform actions in currect sussions (in new windows)?”:
    if I would like to run script in existing SAP session (“yes”)
    if I would like to terminate existing SAP sessions (close) all windows and run script in new one (answer”no”).

    ——————————————————————————
    This is my code currently:
    Sub SapConn()

    Dim Appl As Object
    Dim Connection As Object
    Dim session As Object
    Dim WshShell As Object
    Dim SapGui As Object

    ‘SAP file directory
    Shell “C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe”, 4
    Set WshShell = CreateObject(“WScript.Shell”)

    Do Until WshShell.AppActivate(“SAP Logon “)
    Application.Wait Now + TimeValue(“0:00:01”)
    Loop

    Set WshShell = Nothing

    Set SapGui = GetObject(“SAPGUI”)
    Set Appl = SapGui.GetScriptingEngine

    ‘SAP system
    Set Connection = Appl.Openconnection(“Name”, True)
    Set session = Connection.Children(0)

    ‘ username, password and language
    session.findById(“wnd[0]/usr/txtRSYST-MANDT”).Text = “XXX”
    session.findById(“wnd[0]/usr/txtRSYST-BNAME”).Text = “XXX”
    ‘session.findById(“wnd[0]/usr/pwdRSYST-BCODE”).Text = “password”
    session.findById(“wnd[0]/usr/txtRSYST-LANGU”).Text = “EN”
    session.findById(“wnd[0]”).sendVKey 0

    ‘multiple SAP logons
    On Error Resume Next
    Dim wnd1 As Object
    Set wnd1 = session.findById(“wnd[1]”)
    On Error GoTo 0

    If Not (wnd1 Is Nothing) Then

    Dim answer As Integer
    answer = MsgBox(“Continue?”, vbQuestion + vbYesNo + vbDefaultButton2, _
    “Multiple SAP logons”)

    If answer = vbYes Then
    session.findById(“wnd[1]/usr/radMULTI_LOGON_OPT1”).Select
    session.findById(“wnd[1]/tbar[0]/btn[0]”).press
    Else
    session.findById(“wnd[1]/usr/radMULTI_LOGON_OPT3”).Select
    session.findById(“wnd[1]/tbar[0]/btn[0]”).press
    Exit Sub
    End If
    End If
    ‘end multiple SAP logons

    ‘If session.Children.Count > 1 Then

    ‘answer = MsgBox(“You’ve got opened SAP already,” & _
    ‘”please leave and try again”, vbOKOnly, “Opened SAP”)

    ‘session.findById(“wnd[1]/usr/radMULTI_LOGON_OPT3”).Select
    ‘session.findById(“wnd[1]/usr/radMULTI_LOGON_OPT3”).SetFocus
    ‘session.findById(“wnd[1]/tbar[0]/btn[0]”).press

    ‘Exit Sub

    ‘End If

    session.findById(“wnd[0]”).maximize

    1. Instead of first option You can choose

      2. Continue this session, don’t end others, but some serious stuff is going to be investigated by SAP.

      So replace radMULTI_LOGON_OPT1 for radMULTI_LOGON_OPT2

      Or try to get the existing SAP window object, about which I’ll probably posting in next article 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *