Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

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.

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Joe Smith
      Joe Smith

      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 !

      Author's profile photo Márcio Vieira
      Márcio Vieira

      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.

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Márcio,

      here an approach.

      Cheers

      Stefan

      ;-Begin-----------------------------------------------------------------
      
        ;-Directives----------------------------------------------------------
          AutoItSetOption("MustDeclareVars", 1)
      
        ;-Includes------------------------------------------------------------
          #Include "MsgBoxConstants.au3"
      
        ;-Variables-----------------------------------------------------------
          Global $oError = ObjEvent("AutoIt.Error", "ErrFunc")
      
        ;-Sub ErrFunc---------------------------------------------------------
          Func ErrFunc()
      
            ;>Here you can do what ever you want<
      
            ;-Variables-------------------------------------------------------
              Local $HexNumber
      
            $HexNumber = Hex($oError.number, 8)
            MsgBox($MB_OK, "", "We intercepted a COM Error!" & @CRLF & _
              "Number is: " & $HexNumber & @CRLF & _
              "Description is: " & $oError.windescription)
      
          EndFunc
      
        ;-Sub Main------------------------------------------------------------
          Func Main()
      
            ;-Variables-------------------------------------------------------
              Dim $SAPROT, $SapGuiAuto, $application, $connection, $session
       
            $SAPROT = ObjCreate("SapROTWr.SAPROTWrapper")
            If Not IsObj($SAPROT) Then
              Exit
            EndIf
       
            $SapGuiAuto = $SAPROT.GetROTEntry("SAPGUI")
            If Not IsObj($SapGuiAuto) Then
              Exit
            EndIf
       
            $application = $SapGuiAuto.GetScriptingEngine()
            If Not IsObj($application) Then
              Exit
            EndIf
       
            $connection = $application.Children(0)
            If Not IsObj($connection) Then
              Exit
            EndIf
       
            $session = $connection.Children(0)
            If Not IsObj($session) Then
              Exit
            EndIf
      
            $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")
      
          EndFunc
      
        ;-Main----------------------------------------------------------------
          Main()
      
      ;-End-------------------------------------------------------------------
      
      Author's profile photo Márcio Vieira
      Márcio Vieira

      Dear Stefan,

      Again thank you very much, it worked perfectly here.

      Cheers.

      Márcio.

      Author's profile photo Former Member
      Former Member

      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