0% found this document useful (0 votes)
785 views3 pages

Automating SAP Logon with VBScript

This VBScript connects to an SAP system by starting the SAP Logon process if it is not already running. It uses WMI to check for the SAP Logon process and terminate it. If the process is not found, the script starts SAP Logon, waits for its window to activate, then connects to the SAP system using the IP address and runs a sample script to log in and navigate screens.

Uploaded by

al
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
785 views3 pages

Automating SAP Logon with VBScript

This VBScript connects to an SAP system by starting the SAP Logon process if it is not already running. It uses WMI to check for the SAP Logon process and terminate it. If the process is not found, the script starts SAP Logon, waits for its window to activate, then connects to the SAP system using the IP address and runs a sample script to log in and navigate screens.

Uploaded by

al
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

'-Begin-----------------------------------------------------------------

'-
'- VBScript to start SAP Logon if process doesn't exists and connect
'- to an SAP system. The SAP Logon process is detected via WMI
'- (Windows Management Instrumentarium), also the termination.
'-
'-----------------------------------------------------------------------

'-Directives----------------------------------------------------------
Option Explicit

'-Variables-----------------------------------------------------------
Dim SAPLogon, SAPLogonTitle
SAPLogon = "saplogon.exe" : SAPLogonTitle = "SAP Logon 740"
'SAPLogon = "saplgpad.exe" : SAPLogonTitle = "SAP Logon Pad 740"
Dim SysDescr, SysIP
SysDescr = "NSP" : SysIP = "192.168.203.134"

'-Function FindProcess------------------------------------------------
Function FindProcess(ProcessName)

'-Variables-------------------------------------------------------
Dim WMIServ, Processes, Process

FindProcess = False

Set WMIServ = GetObject("winmgmts:{impersonationLevel=" & _


"impersonate}!\\.\root\cimv2")

Set Processes = WMIServ.ExecQuery("Select * from Win32_Process " & _


"Where Name = '" & ProcessName & "'")

For Each Process In Processes


FindProcess = True
Exit Function
Next

End Function

'-Sub TerminateProcess------------------------------------------------
Sub TerminateProcess(ProcessName)

'-Variables-------------------------------------------------------
Dim WMIServ, Processes, Process

Set WMIServ = GetObject("winmgmts:{impersonationLevel=" & _


"impersonate}!\\.\root\cimv2")

Set Processes = WMIServ.ExecQuery("Select * from Win32_Process " & _


"Where Name = '" & ProcessName & "'")

For Each Process In Processes


Process.Terminate()
Exit Sub
Next

End Sub

'-Function GetSAPGUIObject--------------------------------------------
'-
'- This function starts the SAP Logon if it is necessary and delivers
'- its object
'-
'---------------------------------------------------------------------
Function GetSAPGUIObject()

'-Variables-------------------------------------------------------
Dim WshShell, Exec

If FindProcess(SAPLogon) Then
Set GetSAPGUIObject = GetObject("SAPGUI")
Else
Set WshShell = CreateObject("WScript.Shell")
Set Exec = WshShell.Exec(_
"c:\Program Files (x86)\SAP\FrontEnd\SAPgui\" & SAPLogon)
Do While Not WshShell.AppActivate(SAPLogonTitle)
WScript.Sleep 500
Loop
Set GetSAPGUIObject = GetObject("SAPGUI")
End If

End Function

'-Sub Main------------------------------------------------------------
Sub Main()

'-Variables-------------------------------------------------------
Dim SapGuiAuto, Application, Connection, Session

Set SapGuiAuto = GetSAPGUIObject()


If Not IsObject(SapGuiAuto) Then
Exit Sub
End If

Set Application = SapGuiAuto.GetScriptingEngine


If Not IsObject(Application) Then
Set SapGuiAuto = Nothing
Exit Sub
End If

'-----------------------------------------------------------------
'-
'- Here you have the possibility to decide which way you want to
'- use, on the one hand via system description or on the other
'- hand via IP address
'-
'-----------------------------------------------------------------
'Set Connection = Application.OpenConnection(SysDescr, True)
Set Connection = Application.OpenConnectionByConnectionString(_
"/H/" & SysIP, True)
If Not IsObject(Connection) Then
Set Application = Nothing
Set SapGuiAuto = Nothing
Exit Sub
End If

Set Session = Connection.Children(0)


If Not IsObject(Session) Then
Set Connection = Nothing
Set Application = Nothing
Set SapGuiAuto = Nothing
Exit Sub
End If

MyScript(Session)

Set Session = Nothing


Set Connection = Nothing
Set Application = Nothing
Set SapGuiAuto = Nothing
TerminateProcess SAPLogon

End Sub

'-Sub MyScript--------------------------------------------------------
Sub MyScript(Session)

'-Your script here------------------------------------------------


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

End Sub

'-Main----------------------------------------------------------------
Main

'-End-------------------------------------------------------------------

You might also like