Skip to Content
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:


    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.

You must be Logged on to comment or reply to a post.
  • 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.


    • Hello Márcio,

      here an approach.



          AutoItSetOption("MustDeclareVars", 1)
          #Include "MsgBoxConstants.au3"
          Global $oError = ObjEvent("AutoIt.Error", "ErrFunc")
        ;-Sub ErrFunc---------------------------------------------------------
          Func ErrFunc()
            ;>Here you can do what ever you want<
              Local $HexNumber
            $HexNumber = Hex($oError.number, 8)
            MsgBox($MB_OK, "", "We intercepted a COM Error!" & @CRLF & _
              "Number is: " & $HexNumber & @CRLF & _
              "Description is: " & $oError.windescription)
        ;-Sub Main------------------------------------------------------------
          Func Main()
              Dim $SAPROT, $SapGuiAuto, $application, $connection, $session
            $SAPROT = ObjCreate("SapROTWr.SAPROTWrapper")
            If Not IsObj($SAPROT) Then
            $SapGuiAuto = $SAPROT.GetROTEntry("SAPGUI")
            If Not IsObj($SapGuiAuto) Then
            $application = $SapGuiAuto.GetScriptingEngine()
            If Not IsObj($application) Then
            $connection = $application.Children(0)
            If Not IsObj($connection) Then
            $session = $connection.Children(0)
            If Not IsObj($session) Then
            $session.findById("wnd[0]/usr/txtRSYST-MANDT")      ;Without error
            MsgBox($MB_OK, "", "Now an error occured")
            $session.findById("wnd[0]/usr/txtRSYST-MAND")       ;With error
            MsgBox($MB_OK, "", "Now it goes on")
  • Stefan,

    I am trying to use your code from above.

    ‘-Method Exe——————————————————
    Public Sub Exe(ByVal Cmd)
    CmdField = Split(Cmd, “”””, -1, 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