Skip to Content
2
Jun 30, 2015 at 02:14 PM

Using VBScript for Automated Login to SAP

10282 Views

All,

I'm attempting to create a VBScript that will login to my companies SAP and basically run a report that outputs an HTM file for use with some other reports. Generating the actual report goes off without a hitch, but I would like to set up a chron job (recurring windows task) that would completely automate this process.

I am primarily running into a problem with the automated login to SAP. I have code that can open the SAP executable and I have code that can input the username and password into the login screen but I am at a loss on how to select and execute the target server (it is auto selected at the beginning but sending WshShell.SendKeys "{ENTER}" does not seem to work). Any help would be greatly appreciated!

My code can reviewed on github with comments:

warroom/SAPLogin.vbs at master · PaulStreet/warroom · GitHub

For convenience I have also pasted the code (VBScript below).

'The below section will create an SAP session.

set WshShell = CreateObject("WScript.Shell")

Set proc = WshShell.Exec("C:\Program Files\SAP\FrontEnd\SAPgui\saplogon.exe")

Do While proc.Status = 0

WScript.Sleep 100

Loop

Set SapGui = GetObject("SAPGUI")

Set Appl = SapGui.GetScriptingEngine

''Deprecated alternate code, wait for 6 seconds

'Dim dteWait

'dteWait = DateAdd("s", 6, Now())

'Do Until (Now() > dteWait)

'Loop

'Wait for 5 seconds then press enter.

WScript.Sleep 5000

WshShell.SendKeys "{ENTER}"

''This commented section of code doesn't seem to work for me.

'Set Connection = Appl.Openconnection("Test SAP", True)

'Set session = Connection.Children(0)

'session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "USERNAME"

'session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "PASSWORD"

'session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "E"

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

'The below code is what I can record once I have gotten to the SAP login for the target server.

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-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 = 10

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

Attached is a screenshot of the screen I cannot get past (SAP executable with server select); ie the screen I cannot get past (there are other servers that I have censored out).

Attachments