Wildcards in SAP Selectors (findbyId) - VBScript

Hello There,

I was trying to check if a field in SAP is editable using VBScript. The IDs however seem to be dynamic for each record i was trying to check in a loop. Any idea how to include wildcards in the session.findById command ? Example: The below two selectors are different and i wish to replace these parts with wildcards …

isChangeable = session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0020/ ...").Changeable
isChangeable = session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0022/ ...").Changeable

Thank you
Hara

1 Like

@Hara_Gopal

Hello Hara,
here an approach how to detect an Id of an UI element via regular expression inside VBScript.
Best regards
Stefan

'-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-------------------------------------------------------------------
1 Like