Skip to Content
Technical Articles

Tip: SAP GUI Scripting VBA Code Snippet to Detect all IDs of the UI Elements

In a few cases it is in business context not allowed to install additional analyze software or tools. So applications that are permissible and available must be used, e.g. like Microsoft Office applications. This makes things a bit more laborious, but not impossible.

Here a Visual Basic for Application (VBA) example to detect all IDs of the UI Elements of a session of the SAP GUI for Windows with SAP GUI Scripting. The result is written into a Microsoft Excel table.

The sub routine Start connects to the session and calls the recursive sub routine GetAll, which detects all IDs. The global variable gColl stores all IDs as an array of strings.

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

Option Explicit

Dim gColl() As String
Dim j As Integer

Sub GetAll(Obj As Object) '---------------------------------------------
'-
'- Recursively called sub routine to get the IDs of all UI elements
'-
'-----------------------------------------------------------------------

  Dim cntObj As Integer
  Dim i As Integer
  Dim Child As Object

  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))
      GetAll Child
      ReDim Preserve gColl(j)
      gColl(j) = CStr(Child.ID)
      j = j + 1
    Next
  End If
  On Error GoTo 0

End Sub

Sub Start() '-----------------------------------------------------------
'-
'- Sub routine to get all UI elements of the SAP GUI for Windows
'- with connection 0 and session 0
'-
'-----------------------------------------------------------------------

  Dim SapGuiAuto As Object
  Dim app As SAPFEWSELib.GuiApplication
  Dim connection As SAPFEWSELib.GuiConnection
  Dim session As SAPFEWSELib.GuiSession
  Dim i As Integer

  Set SapGuiAuto = GetObject("SAPGUI")
  If Not IsObject(SapGuiAuto) Then
    Exit Sub
  End If

  Set app = SapGuiAuto.GetScriptingEngine
  If Not IsObject(app) Then
    Exit Sub
  End If

  Set connection = app.Children(0)
  If Not IsObject(connection) Then
    Exit Sub
  End If

  If connection.DisabledByServer = True Then
    Exit Sub
  End If

  Set session = connection.Children(0)
  If Not IsObject(session) Then
    Exit Sub
  End If

  If session.Info.IsLowSpeedConnection = True Then
    Exit Sub
  End If

  GetAll session
  
  For i = LBound(gColl) To UBound(gColl)
    Cells(i + 1, 1) = gColl(i)
  Next

End Sub

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

Here an example of a result:

With the access to all UI elements you can detect each information you need, e.g. like name, type, position, size, etc. On this way you can e.g. easily compare two versions of an UI, to find the differences.

/
4 Comments
You must be Logged on to comment or reply to a post.
  • Very useful code that I was searching for some time. Thanks for sharing.

     

    Please tell me what are the references that I need to add in EXcel VBA window to get this code running?

  • Hi Stefan,

    Very useful script – I made a couple of small changes to get the control Type, Name and Id in a columnar format.

          ReDim Preserve gColl(j)
          gColl(j) = CStr(Child.Type) & ", " & CStr(Child.Name) & ", " & CStr(Child.ID)
          j = j + 1
    

     

      For i = LBound(gColl) To UBound(gColl)
        Cells(i + 1, 1) = Split(gColl(i), ",")(0)
        Cells(i + 1, 2) = Split(gColl(i), ",")(1)
        Cells(i + 1, 3) = Split(gColl(i), ",")(2)
      Next