Technical Articles
How to Find the Next Logon Screen of a SID with SAP GUI Scripting
Based on the discussion here I developed a function how to find the next logon screen of a given SID. The function delivers the connection number.
'-Begin--------------------------------------------------------------
'-Directives-------------------------------------------------------
Option Explicit
'-Function GetConnectionNo-----------------------------------------
'-
'- Function to get connection number of the given SID with
'- logon screen
'-
'------------------------------------------------------------------
Function GetConnectionNo(Application, SID)
'-Variables----------------------------------------------------
Dim i, Connection, SessionInfo, ConnectionNo
If Application.Children.Count > 0 Then
For i = 0 To Application.Children.Count - 1
Set Connection = Application.Children(CInt(i))
Set SessionInfo = Connection.Children(0).Info
If SessionInfo.SystemName = SID And _
SessionInfo.Program = "SAPMSYST" Then
ConnectionNo = Mid(Connection.Id, InStr(Connection.Id, "[") + 1)
ConnectionNo = Left(ConnectionNo, Len(ConnectionNo) - 1)
GetConnectionNo = CStr(ConnectionNo)
End If
Next
Else
GetConnectionNo = -1
End If
End Function
'-Sub Main---------------------------------------------------------
Sub Main()
'-Variables----------------------------------------------------
Dim SapGuiAuto, Application, ConnNo
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
End If
ConnNo = GetConnectionNo(Application, "NSP")
MsgBox ConnNo
End Sub
'-Main-------------------------------------------------------------
Main
'-End----------------------------------------------------------------
Hi Stefan,
I could get this code to work, but only a portion of it and I get the connection number with:
Set SessionInfo = Connection.Children(0).Info
If SessionInfo.SystemName = SID And _
SessionInfo.Program = "SAPMSYST" Then
ConnectionNo = Mid(Connection.ID, InStr(Connection.ID, "[") + 1)
ConnectionNo = Left(ConnectionNo, Len(ConnectionNo) - 1)
ConnNo = CStr(ConnectionNo)
End If
The ConnNo variable is incrementing correctly.
I have a lot of automation so I'm handling it like this: a wrapper takes care of the logon/logoff and calls any requested script inside.
So the ConnNo is retrieved in the wrapper script and passed into the called specific scripts. I've changed my calls to objects from
session.findById("wnd[0]/usr/txt[0]").Text
to
session.findById("/app/con[" & ConnNo & "]/ses[0]/wnd[0]/usr/txt[0]").Text
This works in the wrapper and I can logon to several systems. But it doesn't work in the scripts called by the wrapper to execute specific actions in the system. For example:
Sub displ_statistics_worklist(worklist As Variant, var_group As Variant, source_language, target_language, conn_no As Variant)
Set SapGuiAuto = GetObject("SAPGUI") 'Get the SAP GUI Scripting object
Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
.
.
more code
Any ideas why this call fails? I suspect the issue lies in this line:
Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
But even after I've tried passing the ConnNo as argument, it still fails.
Kind regards,
Sebastjan
Hello Sebastjan,
please try the following changes in your sub routine:
Set SAPCon = SAPApp.Children(CLng(ConnNo))
Set session = SAPCon.Children(0)
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "slls"
session.findById("wnd[0]").sendVKey 0
On this way you set your SAPCon variable to your connection number from the parameter ConnNo. So it is not longer necessary to set the complete ID.
Let us know your results.
Cheers
Stefan
Hello Stefan,
your solution works like a charm 🙂
Thank you very much!
Kind regards,
Sebastjan
Hi Stefan
I need help for one of my SAP systems
I am trying two VBS scripts in the below to first open SWM system session from SAP logon Pad and then pass the second script which is used to login to the system to using sap*
First script is working well but second script is not working when used along with Script1!
Can you please
////////////////////////////////////////////////////////////////////////////////////////////
'-Begin-----------------------------------------------------------------
'-Directives----------------------------------------------------------
Option Explicit
'-Variables-----------------------------------------------------------
Dim WSHShell, SAPGUIPath, SID, InstanceNo, WinTitle
'-Main----------------------------------------------------------------
Set WSHShell = WScript.CreateObject("WScript.Shell")
If IsObject(WSHShell) Then
'-Set the path to the SAP GUI directory---------------------------
SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"
'-Set the SAP system ID-------------------------------------------
SID = "ldciswf
'-Set the instance number of the SAP system-----------------------
InstanceNo = "15"
'-Starts the SAP GUI----------------------------------------------
WSHShell.Exec SAPGUIPath & "sapgui.exe " & SID & " " & _
InstanceNo
'-Set the title of the SAP GUI window here------------------------
WinTitle = "SAP"
While Not WSHShell.AppActivate(WinTitle)
WScript.Sleep 250
Wend
Set WSHShell = Nothing
End If
////////////////////////////////////////
SCRIPT 2:
//////////////////////////////////////
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
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "/nsu01"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtSUID_ST_BNAME-BNAME").text = "SAP*"
session.findById("wnd[0]/usr/ctxtSUID_ST_BNAME-BNAME").caretPosition = 4
session.findById("wnd[0]/tbar[1]/btn[18]").press
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO").select
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/pwdSUID_ST_NODE_PASSWORD_EXT-PASSWORD").text = "*******"
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/pwdSUID_ST_NODE_PASSWORD_EXT-PASSWORD2").text = "******"
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/pwdSUID_ST_NODE_PASSWORD_EXT-PASSWORD2").setFocus
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/pwdSUID_ST_NODE_PASSWORD_EXT-PASSWORD2").caretPosition = 8
session.findById("wnd[0]/tbar[0]/btn[11]").press
//////////////////////////////////////////////////////////
Hello Ravindranath,
delete the directive Option Explicit from your code please and now it should work.
Cheers
Stefan
Thanks a lot it worked 🙂
Hi Stefan,
It worked for once but know i am getting below error
Hello Ravindranath,
I don't know exactly your requirement and your code, but if you open the same system multiple times and e.g. you close the first connection but with the command application.Children(0) in your script you want to connect the first you get this error. Also it is necessary to set the correct WinTitle e.g. like "ldciswf(1)/000 SAP".
Cheers
Stefan
Hi Stefan,
Need help here its urgent for one of implementation project ..i am actually trying to just login automatically using KUMARRAVI4 user and run a report with the below script by its own
But it keeps on giving me below different error everytime
1>Session id not found :>is there any away i can skip this ?
2>sometimes like below
3>or sometimes it just goes and stucks to login page and wont go forward ....
///////////////////////////////////
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
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-MANDT").text = "000"
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "KUMARRAVI4"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "pavan123"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 10
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/okcd").text = "/nstrust"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/shellcont/shell").expandNode "PROG<SYST>"
session.findById("wnd[0]/shellcont/shell").topNode = "PROG<SYST>"
session.findById("wnd[0]/shellcont/shell").expandNode "SSLSDFAULT"
session.findById("wnd[0]/shellcont/shell").topNode = "PROG<SYST>"
session.findById("wnd[0]/shellcont/shell").expandNode "SSLCANONYM"
session.findById("wnd[0]/shellcont/shell").topNode = "PROG<SYST>"
session.findById("wnd[0]/tbar[0]/okcd").text = "/nact_test"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]/usr/cmbP_LAND").key = "2"
session.findById("wnd[1]/usr/cmbP_TYP").key = "2"
session.findById("wnd[1]/usr/cmbP_TYP").setFocus
session.findById("wnd[1]/tbar[0]/btn[8]").press
session.findById("wnd[1]/usr/btnBUTTON_1").press
session.findById("wnd[0]/usr/cntlCON_MENU/shellcont/shell/shellcont[1]/shell[1]").expandNode " 1"
session.findById("wnd[0]/usr/cntlCON_MENU/shellcont/shell/shellcont[1]/shell[1]").topNode = " 1"
session.findById("wnd[0]/usr/cntlCON_MENU/shellcont/shell/shellcont[1]/shell[1]").expandNode " 3"
session.findById("wnd[0]/usr/cntlCON_MENU/shellcont/shell/shellcont[1]/shell[1]").topNode = " 1"
session.findById("wnd[0]/usr/cntlCON_MENU/shellcont/shell/shellcont[1]/shell[1]").expandNode " 20"
session.findById("wnd[0]/usr/cntlCON_MENU/shellcont/shell/shellcont[1]/shell[1]").topNode = " 1"
session.findById("wnd[0]/usr/cntlCON_MENU/shellcont/shell/shellcont[1]/shell[1]").selectedNode = " 21"
session.findById("wnd[0]/usr/cntlCON_MENU/shellcont/shell/shellcont[1]/shell[1]").doubleClickNode " 21"
session.findById("wnd[0]/usr/ssubSAR:ACT_DEV_TEST:0902/cntlALLCON/shellcont/shell").setCurrentCell 4,""
session.findById("wnd[0]/usr/ssubSAR:ACT_DEV_TEST:0902/cntlALLCON/shellcont/shell").selectedRows = "4"
session.findById("wnd[0]/usr/ssubSAR:ACT_DEV_TEST:0902/cntlALLCON/shellcont/shell").doubleClickCurrentCell
session.findById("wnd[1]/tbar[0]/btn[13]").press
session.findById("wnd[1]/usr/chkC_TESTER").selected = false
session.findById("wnd[1]/usr/chkC_AALE").selected = true
session.findById("wnd[1]/usr/chkC_ATESTER").selected = true
session.findById("wnd[1]/usr/chkC_ACATTER").selected = false
session.findById("wnd[1]/usr/cmbOPACKAGE").key = "004"
session.findById("wnd[1]/usr/chkC_ACATTER").setFocus
session.findById("wnd[1]").sendVKey 2
session.findById("wnd[0]/usr/ssubSAR:ACT_DEV_TEST:0902/cntlALLCON/shellcont/shell").setCurrentCell 4,""
session.findById("wnd[0]/usr/ssubSAR:ACT_DEV_TEST:0902/cntlALLCON/shellcont/shell").selectedRows = "4"
session.findById("wnd[0]/usr/ssubSAR:ACT_DEV_TEST:0902/cntlALLCON/shellcont/shell").doubleClickCurrentCell
session.findById("wnd[1]/usr/chkC_OPLO").selected = true
session.findById("wnd[1]/usr/chkC_ACRMOLTP").selected = true
session.findById("wnd[1]/usr/chkC_AALE").selected = true
session.findById("wnd[1]/usr/chkC_ATESTER").selected = true
session.findById("wnd[1]/usr/chkC_ACATTER").selected = true
session.findById("wnd[1]/usr/chkC_AANZEIGER").selected = true
session.findById("wnd[1]/usr/chkC_AANZEIGER").setFocus
session.findById("wnd[1]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/ssubSAR:ACT_DEV_TEST:0902/cntlALLCON/shellcont/shell").setCurrentCell 4,""
session.findById("wnd[0]/usr/ssubSAR:ACT_DEV_TEST:0902/cntlALLCON/shellcont/shell").selectedRows = "4"
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "********"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 10
session.findById("wnd[0]").sendVKey 0
/////////////////////////////////////////////////////////////
Hello Pavan,
I hope I could help you.
Cheers
Stefan
And is there a way in which it can login using SNC ?
Say it just has to double click as shown below and click on client for which we need to be logged in
Thanks
Pavan
Hello Pavan,
I don't have any experience to use Secure Network Communications (SNC) in the context of SAP GUI Scripting. All I know is the setting in the SAP Logon:
Whether this has an effect on the SAP GUI scripting, I can't say.
Let us know your experience, it is very interesting to know.
Cheers
Stefan