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

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

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.
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
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
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
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
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
Instead of first option You can choose
So replace
radMULTI_LOGON_OPT1
forradMULTI_LOGON_OPT2
Or try to get the existing SAP window object, about which I’ll probably posting in next article 🙂
Hello Tomasz,
Thanks for your artical and videos, they are really helpful.
I have one question here regarding SAP login options, right click my ‘SAP system’ has two login options, one is ‘single sign-on, can login SAP without password’; another one is ‘without single sign-on, need to log in with password’ for higher access level.
How can i use VBA msg box to toggle between the two login options ?
If MsgBox(“Do you wish to login with password?”, vbYesNo) = vbYes Then
GoTo 10 ‘Without single sign-on
Else
GoTo 11 ‘Single sign-on SAP
End If
Hello Wilmer
How is working the code You showed? 🙂
Hi,
here is a piece of my code,
it works fine when i am logged in, in one session.
if i run the script again while being logged in, the window to enter the name and password will pop up, but throw a 619 not found by id error afterwards (error is thrown at the sessionFindById line).
so i cant even get to the multilogon part.
Can you think of any reason for this?
thanks in advance
Sub SAPlogin()
Dim SapGUI As Object
Dim appl As SAPFEWSELib.GuiApplication
Dim session As SAPFEWSELib.GuiSession
Dim WshShell As Object
Dim Conn As SAPFEWSELib.GuiConnection
Shell “C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe”, 4
Set WshShell = CreateObject(“WScript.Shell”)
WshShell.AppActivate (“SAP Logon”)
Application.Wait Now + TimeValue(“00:00:08”)
Set WshShell = Nothing
Set SapGUI = GetObject(“SAPGUI”)
Set appl = SapGUI.GetScriptingEngine
Set Conn = appl.OpenConnection(“fullpathname”)
Set Conn = appl.Children(0)
Set session = Conn.Children(0)
session.FindById(“wnd[0]/usr/txtRSYST-BNAME”).Text = “name”
etc…
Hello
I can’t see anything in your code, which is mentioned in article about multilogons.
That should be the reason.
Try to implement any example, eg check number of sessions, check if any additional window pop up etc.
thank for good article
Thank You 🙂
Hi Tomasz,
I wrote a code for generating report from sap. all the reports work fine when the different vba code is run separately. how can I modify the code to just create a new GUI window and run the transaction normally.
for example
I run a macro, it logs into sap and generates a report.
what I want to implement is —-
Now I dont want to log out from that session. i want to create a new window and run a different t-code in sap.
please tell me how can implement that.
Dim SapGuiAuto As Object, Application As Object, Connection As Object, session As Object
Set SapGuiAuto = GetObject(“SAPGUI”)
Set Application = SapGuiAuto.GetScriptingEngine
Set Connection = Application.Children(0)
Set session = Connection.Children(0)
session.findById(“wnd[0]”).maximize
session.findById(“wnd[0]/usr/txtRSYST-BNAME”).Text = ‘I enter login id here
session.findById(“wnd[0]/usr/pwdRSYST-BCODE”).Text = ‘I enter password here
session.findById(“wnd[0]/usr/pwdRSYST-BCODE”).SetFocus
session.findById(“wnd[0]/usr/pwdRSYST-BCODE”).caretPosition = 11
session.findById(“wnd[0]”).sendVKey 0
session.findById(“wnd[1]/usr/btnBUTTON_1”).press
session.findById(“wnd[0]/tbar[0]/okcd”).Text = ‘I enter t-code here
session.findById(“wnd[0]”).sendVKey 0
and then the vba code enters the details of that particular sap t-code.
Hello
You need to try to create another session and do what You want to do parallelly there.