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).
Hello Paul,
welcome in the scripting forum.
You can use this VBScript to open a connection to an SAP system.
'-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 = "NSP"
'-Set the instance number of the SAP system-----------------------
InstanceNo = "00"
'-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
MsgBox "Here now your script..."
'-End-------------------------------------------------------------------
So it is not necessary to work with the SAP Logon or SAP Logon Pad directly. But if it is required you find on my blog an older solution from 19.02.2009 with AutoIt.
Let us know your results.
Cheers
Stefan
Add a comment