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

How to Detect UI Element ID with Regular Expressions in SAP GUI Scripting

Often SAP GUI scripting users report problems using UI elements of SAP GUI applications that include the program name and screen number in their ID.

Here an example:


In this ID we see the window wnd[0], the user screen usr and now follows a simple container with the program name and the screen number. In our example is it BRF_MAINTENANCE for the program name, with the prefix SAPL, and the screen number 3006. Last but not least follows the UI element ID.

In some cases it is possible that the screen number change, e.g. from 3006 to 3007. If this ID is now used in a SAP GUI script, an error occurs because the ID has changed.

To deal with this fact a little bit better I developed a small VBScript routine to search for parts of an ID, using Regular Expressions. To search for the ID in the example the following can be used:


In this case without the program name and the screen number. On this way IDs of UI elements, without a variable part such as the screen number, can be found. Use the function FindByIdPart in your SAP GUI Scripting code. It is a recursive function that searches the specified container and returns the ID of the found element as a string. The use of Regular Expressions opens up many more possibilities.


'-Function FindByIdPart-------------------------------------------------
'- Function to find an UI element by its Id via Regular Expressions,
'- independently from program names and screen numbers
'- oApp = SAP application
'- oArea = Container to be searched
'- regexId = Regular Expression of Id of UI element which is searched
Function FindByIdPart(oApp, oArea, regexId)

  Set oRegEx = New RegExp
  oRegEx.Pattern = regexId
  oRegEx.IgnoreCase = True
  oRegEx.Global = False

  On Error Resume Next
  If oArea.Children.Count() > 0 Then
    For i = 0 To oArea.Children.Count() - 1
      Set Child = oArea.Children.Item(CLng(i))
      If oRegEx.Test(Child.Id) Then
        FindByIdPart = Child.Id
        On Error GoTo 0
        Exit Function
      End If
      If Child.ContainerType() And Child.Children().Count() > 0 Then
        FindByIdPart = _
          FindByIdPart(oApp, oApp.findByID(Child.Id), regexId)
        If FindByIdPart <> "" Then
          On Error GoTo 0
          Exit Function
        End If
      End If
  End If
  On Error Goto 0
  FindByIDPart = ""

End Function

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

  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

  app.HistoryEnabled = False

  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(1)
  If Not IsObject(session) Then
    Exit Sub
  End If

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

  'Search for
  Id = FindByIDPart(app, session.findById("wnd[0]/usr"), _

  MsgBox Id

  app.HistoryEnabled = True

End Sub




Assigned Tags

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