Skip to Content
0
Dec 22, 2022 at 02:50 AM

Auto Log in using vbscript

514 Views Last edit Dec 22, 2022 at 03:02 AM 8 rev

<code>Option Explicit
    Dim WSHShell, SAPGUIPath, SID, InstanceNo, WinTitle, Name
    Set WSHShell = WScript.CreateObject("WScript.Shell")
    If IsObject(WSHShell) Then
    SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"
    
    Name = TestName
    InstanceNo = "00"
    WSHShell.Exec SAPGUIPath & "sapgui.exe " & Name & " " & _
      InstanceNo

    WinTitle = "SAP" 
    While Not WSHShell.AppActivate(WinTitle)
      WScript.Sleep 250
    Wend

    Set WSHShell = Nothing
    
    End If

    'Delay for the log in window
    WScript.Sleep 5000


    Dim SAPGUI, proc, Appl, application, SapGuiAuto, connection, session

    'The below section will create an SAP session.
    set WshShell = CreateObject("WScript.Shell")
    Set proc = WshShell.Exec("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe")
            Do While proc.Status = 0
            WScript.Sleep 100
            Loop
    
    Set session = StablishSession() 'Set up session with SAP
    
    If not session is Nothing then 'If connection was stablished to SAP

    session.findById("wnd[0]").maximize
    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 = 12
    session.findById("wnd[0]").sendVKey 0

    WSHShell.SendKeys "%{TAB}" 'switch active window - alt tab

    End if



    'create session in SAP
    Function StablishSession
    On Error Resume Next

    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 Err.Number <> 0 Then
    Msgbox "SAP Session must be open first"
     Set StablishSession = Nothing
    On Error Goto 0

    Else
    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

    Set StablishSession = session

    end if
    End Function


This code works only if there's no active session yet. But once there's an active session the script failed to work. The problem I notice is the switch of windows in SAP making the script failed to locate the needed elements. Will I be able to prevent this switch in windows and just focus on the current window that has been opened by the script? You can see that I also use alt tab at the end so that the window I needed will show in front. Thanks in advance.

The vbscript is able to open the window with the field of username and password but unable to input the username and password because it switches to the previously opened windows. Let's say I have 1 active session (window 1) then I run this vbscript. It will open window 2 but it failed to log in because the vbscript switches the active window to window 1. I'm expecting that it will stay to window 2 so that it will be able to log in the username and password.

As an additional details, I am not able to assign a title to each and every window. WinTitle is fix to "SAP". So if I use WSHShell.AppActivate(WinTitle), it opens window 1.

I based this code to the link below: How to Logon on SAP with SSO via VBS?