cancel
Showing results for 
Search instead for 
Did you mean: 

Loop through SAPGUI fields in current window using VBA

Former Member
0 Kudos

Using VBA, I've recently found out the way below to select the desired TAB (when within a sales order, for instance).




For T = 0 To 15
  
If Len(T) = 1 Then T = "0" & T
  
If SapSession.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\" & T).Text = "Sales" Then
        SapSession
.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\" & T).Select
  
Exit For
  
End If
Next T



I am looking now for a similar way to loop through the fields in the current active window in order to select (setfocus) on a specific field. Is it possible?


Thank you

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor
0 Kudos

Hello Levy,

welcome in the Scripting Language forum.

Here a solution to scan the entire user screen and to get all elements:

'-Begin-----------------------------------------------------------------
Sub ScanFields(Area As Object, Application As SAPFEWSELib.GuiApplication)
 
  Dim Children As Object
  Dim i As Integer
  Dim Obj As Object
  Dim ObjChildren As Object
  Dim NextArea As Object
 
  Set Children = Area.Children()
  For i = 0 To Children.Count() - 1
    Set Obj = Children(CInt(i))
    'If Obj.Type = "GuiTextField" Then
      'If Obj.Name = "MyField" Then
        'Obj.SetFocus
        Debug.Print Obj.Name & " " & Obj.Type & " " & Obj.Text
      'End If
    'End If
     
    If Obj.ContainerType() = True Then
      Set ObjChildren = Obj.Children()
      If ObjChildren.Count() > 0 Then
        Set NextArea = Application.findByID(Obj.ID)
        ScanFields NextArea, Application
        Set NextArea = Nothing
      End If
      Set ObjChildren = Nothig
    End If
    Set Obj = Nothing
  Next
  Set Children = Nothing
 
End Sub

'---------------------------------------------------------------------
Sub Test()

  Dim SapGuiAuto As Object
  Dim Application As SAPFEWSELib.GuiApplication
  Dim Connection As SAPFEWSELib.GuiConnection
  Dim Session As SAPFEWSELib.GuiSession
  Dim UserArea As SAPFEWSELib.GuiUserArea

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

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

  Set Connection = Application.Connections(0)
  If Not IsObject(Connection) Then
    Exit Sub
  End If

  Set Session = Connection.Sessions(0)
  If Not IsObject(Session) Then
    Exit Sub
  End If

  '-Get the user area and scan it recursively---------------------------
  Set UserArea = Session.findByID("wnd[0]/usr")
  ScanFields UserArea, Application
  Set UserArea = Nothing

End Sub

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

I commented the lines in the sub routine ScanFields to select your specific field and to set the focus.

It is necessary to check if an object is a container and to scan the objects in that case recursively

Let us know your results.

Cheers

Stefan

0 Kudos

@stefan.schnell

Can we do same thing for Grid - shell container as I checked above code it ignore GuiGridView objects??

stefan_schnell
Active Contributor
0 Kudos

Hello klambe,

the code doesn't ignore GridViews. A GridView is one object on the user screen. It contains no objects which can be determined with this method. If you need detail information about the content of the GridView, it is necessary to use the methods and properties of the GuiGridView object.

Best regards
Stefan

Answers (0)