Skip to Content
Technical Articles

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:

wnd[0]/usr/subSSA1:SAPLBRF_MAINTENANCE:3006/txtSBRF170-VERSION

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:

wnd\[0\]\/usr\/subSSA1:.*txtSBRF170-VERSION

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.

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

'-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
    Next
  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
  'wnd[0]/usr/subSSA1:SAPLBRF_MAINTENANCE:3006/txtSBRF170-VERSION
  Id = FindByIDPart(app, session.findById("wnd[0]/usr"), _
    "wnd\[0\]\/usr\/subSSA1:.*txtSBRF170-VERSION")

  MsgBox Id

  app.HistoryEnabled = True

End Sub

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

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

 

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