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

Function FindAllByType and FindAllByTypeEx in SAP GUI Scripting

In this post I present two functions to detect all controls of one type of a collection, called FindAllByType and FindAllByTypeEx. The function FindAllByType has the parameters obj which describes a collection of objects and strType which describes the type of the control as string, e.g. GuiLabel. The function FindAllByTypeEx has the parameters obj which describes a collection of objects and lngType which describes the type of the control as long, e.g. 30 for GuiLabel. Both delivers an array of objects of the required type of the control.

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

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

  '-Global Variables----------------------------------------------------
    Dim gColl()

  '-Function FindAllByType----------------------------------------------
    Function FindAllByType(Obj, strType)

      '-Variables-------------------------------------------------------   
        Dim cntObj, i, j, Child

      On Error Resume Next    
      cntObj = Obj.Children.Count()    
      If cntObj > 0 Then    
        For i = 0 To cntObj - 1    
          Set Child = Obj.Children.Item(CLng(i))    
          FindAllByType Child, strType
          If UCase(Child.Type()) = UCase(strType) Then
            ReDim Preserve gColl(j)
            Set gColl(j) = Child
            j = j + 1
          End If
        Next    
      End If    
      On Error Goto 0  
      FindAllByType = gColl

    End Function

  '-Function FindAllByType----------------------------------------------
    Function FindAllByTypeEx(Obj, lngType)

      '-Variables-------------------------------------------------------   
        Dim cntObj, i, j, Child

      On Error Resume Next    
      cntObj = Obj.Children.Count()    
      If cntObj > 0 Then    
        For i = 0 To cntObj - 1    
          Set Child = Obj.Children.Item(CLng(i))    
          FindAllByTypeEx Child, lngType
          If Child.TypeAsNumber() = lngType Then
            ReDim Preserve gColl(j)
            Set gColl(j) = Child
            j = j + 1
          End If
        Next    
      End If    
      On Error Goto 0  
      FindAllByTypeEx = gColl

    End Function

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

      '-Variables-------------------------------------------------------
        Dim SapGuiAuto, application, connection, session, Coll, i
        Dim OutText

      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 

      Erase gColl
      Coll = FindAllByType(session, "GuiLabel")
      For i = 0 To UBound(Coll)
        OutText = OutText & Coll(i).ID() & vbCrLf
      Next

      Erase gColl
      Coll = FindAllByTypeEx(session, 31) 'GuiTextField
      For i = 0 To UBound(Coll)
        OutText = OutText & Coll(i).ID() & vbCrLf
      Next

      MsgBox OutText

    End Sub

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

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

Enjoy it.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.