Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
Showing results for 
Search instead for 
Did you mean: 
Active Contributor
Here a method how to check the existence of an object in SAP GUI Scripting. In a normal case, if an object not exists, you get an exception error in a messagebox and the SAP GUI script terminate immediately. With this method you can choose whether the SAP GUI script terminate or not. Here the source:

Option Explicit
On Error Resume Next

'-Global Variables----------------------------------------------------
Dim SapGuiAuto, application, connection, session, QuitFlag

'-Class cCheck--------------------------------------------------------
Class cCheck

'-Instance Variables----------------------------------------------
Dim CmdField

'-Method Exe------------------------------------------------------
Public Sub Exe(ByVal Cmd)
CmdField = Split(Cmd, """", -1, 1)
ExecuteGlobal Cmd
End Sub

'-Method ErrCatch-------------------------------------------------
Private Sub ErrCatch
If Err.Number = 0 Then Exit Sub
Select Case Err.Number
Case 619
If MsgBox("Run-time error " & Err.Number & _
" occurred." & vbCrLf & Err.Description & vbCrLf & _
"ID: " & CmdField(1) & vbCrLf & vbCrLf & "Abort?", _
vbYesNo, "Error on " & Err.Source) = vbYes Then
QuitFlag = True
End If
Case Else
If MsgBox("Run-time error " & Err.Number & _
" occurred." & vbCrLf & Err.Description & vbCrLf & _
vbCrLf & "Abort?", vbYesNo, "Error on " & _
Err.Source) = vbYes Then
QuitFlag = True
End If
End Select
End Sub

Private Sub Class_Terminate
End Sub

End Class

'-Sub C---------------------------------------------------------------
Sub C(ByVal Cmd)
Dim Check
If QuitFlag Then WScript.Quit()
Set Check = New cCheck : Check.Exe Cmd : Set Check = Nothing
End Sub

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

C "session.findById(""wnd[0]/usr/txtRSYST-MANDT"").text = ""001"""
'-The following object doesn't exists---------------------------------

C "session.findById(""wnd[0]/usr/txtRSYST-MAND"").text = ""001"""
C "session.findById(""wnd[0]/usr/txtRSYST-BNAME"").text = ""bcuser"""
C "session.findById(""wnd[0]/usr/pwdRSYST-BCODE"").text = ""minisap"""
C "session.findById(""wnd[0]/usr/txtRSYST-LANGU"").text = ""EN"""
C "session.findById(""wnd[0]/tbar[0]/btn[0]"").press"



The sub procedure C sends the SAP GUI Scripting command as string parameter and delivers it to the method Exe of the Check class. This method call the method FindById and if it doesn't find, an exception is thrown. Now the class was destroyed and the destructor calls the method ErrCatch. This method opens a messagebox and sets a global flag. If the flag is set, with the positive answer of the messagebox, the script terminate immediately or runs again with a negative answer. In the example above I check the objects of the logon screen.

I think this method is a way to make SAP GUI Scripting more robust for the work in different SAP environments.
Labels in this area