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

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.

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Kannan NP
      Kannan NP

      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?

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

      Kannan NP

      Hello Kannan,

      you have only to reference to SAPFEWSE.OCX library.

      Best regards
      Stefan

      Author's profile photo Andrew Frigaard
      Andrew Frigaard

      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

       

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

      Thanks for sharing Andrew

      Author's profile photo Hara Kovvali
      Hara Kovvali

      Hello Stefan,

      am kind of new with SAP scripting. I used this script, but it say "End of statement expected". Can you please tell me how to run this script correctly

      Thank you and appreciate you help

      Hara

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

      Hello Hara,

      please check if you copy the code completely, with no additional characters and correct spaces. The "End of statement expected" means that the statement is syntactically complete, but an additional programming element follows the element that completes the statement.

      Best regards
      Stefan

      Author's profile photo ZHANG LU
      ZHANG LU

      very userful and I will try it tommorrow