Technical Articles
How to Check the Existence of an Object Without Exception Messagebox
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:
'-Begin-----------------------------------------------------------------
'-Directives----------------------------------------------------------
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)
session.findById(CmdField(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
Err.Clear
End Sub
'-Destructor------------------------------------------------------
Private Sub Class_Terminate
ErrCatch
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
'-Main----------------------------------------------------------------
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"
'-End-------------------------------------------------------------------
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.
Thanks, I was looking for exactly this !
I have a situation where sometimes a dialog will popup, but sometimes not !
My current solution was very inelegant, I would ask the user if the dialog was there ! If they lie, the program crashes !
With this I will not need to ask at all !
thanks again !
Hello Stefan,
Here I am again need your help ...
... Need to check if the result contains data in the user area, through guilabel object () but I can not, I found this post and I believe it will be helpful but the language is in VBScript would be possible to post in AutoIt?
Thank you very much.
Márcio.
Hello Márcio,
here an approach.
Cheers
Stefan
Dear Stefan,
Again thank you very much, it worked perfectly here.
Cheers.
Márcio.
Stefan,
I am trying to use your code from above.
'-Method Exe------------------------------------------------------
Public Sub Exe(ByVal Cmd)
CmdField = Split(Cmd, """", -1, 1)
session.findById(CmdField(1))
ExecuteGlobal Cmd
End Sub
When it gets to ExecuteGlobal Cmd... it is showing error?
"Sub or Function not defined?
I know this is a very old post... if you can help that would be wonderful. Thanks, David