Sap Logon automate login using vsbcript

Malbordio picture Malbordio · May 9, 2018 · Viewed 8.3k times · Source

I am using a VBScript to login automatically into SAP GUI. It opens automatically the SAP GUI window, it loads the SAP server, but it doesn't populate automatically the user and password fields (remain blank).

It also gives a script error on line 52, char 4:

The enumerator of the collection cannot find en element with the specified index.

The code is the following:

REM The following script was written to log into the SAP server automatically.
REM To view historical information and credit for this script please see
REM the following thread on the SAP Community Network:
REM http://scn.sap.com/thread/3763970

REM This script was last updated by Paul Street on 7/1/15

REM Directives
    Option Explicit

  REM Variables!  Must declare before using because of Option Explicit
    Dim WSHShell, SAPGUIPath, SID, InstanceNo, WinTitle, SapGuiAuto, application, connection, session

  REM Main
    Set WSHShell = WScript.CreateObject("WScript.Shell")
    If IsObject(WSHShell) Then

      REM Set the path to the SAP GUI directory
        SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"

      REM Set the SAP system ID
        SID = "eaiserver.domain.com"

      REM Set the instance number of the SAP system
        InstanceNo = "38"

      REM Starts the SAP GUI
        WSHShell.Exec SAPGUIPath & "SAPgui.exe " & SID & " " & _
          InstanceNo
 
      REM 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
 
    REM Remove this if you need to test the above script and want a message box at the end launching the login screen.
    REM MsgBox "Here now your script..." 
     
    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 = "100"
    session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "I'veInsertedtheCorrectUsernameHere"
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "I'veInsertedtheCorrectPassHere"
    session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "PT"
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 10
    session.findById("wnd[0]").sendVKey 0

Thanks for the help!

Answer

Angelo Bovino picture Angelo Bovino · Aug 14, 2018

I am using the exact same script, which I found somewhere on the SAP help forums.

When I've encountered this issue before it's usually because the SAP GUI window was loaded AFTER lines

session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "username"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "password"

There are two ways to fix this particular script. One is to add a MsgBox that will pause the script, but give SAP GUI enough time to load. The other is to add WScript.Sleep(<a few seconds>) to allow SAP GUI to load. Like so ...

Note that the below code has BOTH examples, but only 1 is necessary. I prefer the .Sleep() because it requires no external input from a user.

If IsObject(WSHShell) Then
    ' Removed for clarity
End If

MsgBox "Click OK to continue" ' <-- MsgBox to pause script
WScript.Sleep(5000) ' <--- Wait 5 seconds for SAP GUI to load

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]").resizeWorkingPane 164,40,false
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "username"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "password"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 14
session.findById("wnd[0]").sendVKey 0

And of course, storing username and password in plain text is not a good practice. However, obfuscating passwords with the VBScript InputBox() is not possible. You will have to use the command line, or create an IE object which is outside the scope of this question