Vinnie
Vinnie

Reputation: 553

Get field name in SAP GUI

I'm trying to get this field name in SAP GUI:

session.findById("wnd[0]/usr/subBLOCK:SAPLKACB:1015/ctxtCOBL-KOSTL")

I have the following code, to check each object in the given screen:

Option Explicit
Sub SAPfields()

    Dim sapguiauto As Object
    Dim sapapp As Object
    Dim sapcon As Object
    Dim session As Object
    Dim Area As Object
    Dim i As Long
    Dim Children As Object
    Dim Obj As Object

    Set sapguiauto = GetObject("SAPGUI")
    Set sapapp = sapguiauto.GetScriptingEngine
    Set sapcon = sapapp.Children(0)
    Set session = sapcon.Children(0)
    Set Area = session.findById("wnd[0]/usr")
    Set Children = Area.Children()

    For i = 0 To Children.Count() - 1
        Set Obj = Children(CInt(i))
        Debug.Print Obj.Name
                
    Next i
    Set Children = Nothing
    Set Obj = Nothing
    
End Sub

When I print the object names, I get this:

subBLOCK:SAPLKACB:1015

What should I do to get the exact field?

I tried to use below code but it did not work:

OBJ("subBLOCK:SAPLKACB:1015").Children()

Upvotes: 1

Views: 3902

Answers (2)

mherzog
mherzog

Reputation: 1230

On my system (SAP front end version 7400.3.13.3369), executing your code gives me the field name, but it is not fully qualified.

E.g. Here's selection criteria from a SQVI screen on my system.

selection criteria from a SQVI screen on my system

Running your procedure with this screen visible, I get the following:

%BS02000_BLOCK_1000
%_SP$00001_%_APP_%-TEXT
SP$00001-LOW
%_SP$00001_%_APP_%-TO_TEXT
...

You can simply change your code to display the Obj.ID as shown below to get the fully qualified path to the field name.

...
debug.print Obj.ID
...

This gives

/app/con[0]/ses[0]/wnd[0]/usr/box%BS02000_BLOCK_1000
/app/con[0]/ses[0]/wnd[0]/usr/txt%_SP$00001_%_APP_%-TEXT
/app/con[0]/ses[0]/wnd[0]/usr/ctxtSP$00001-LOW
/app/con[0]/ses[0]/wnd[0]/usr/txt%_SP$00001_%_APP_%-TO_TEXT
...

If you want to get the text displayed on the screen, use .Text:

...
debug.print "Name=" & Obj.Name & "  Value=" & Obj.Text
...
Name=%_SP$00001_%_APP_%-TEXT  Value=Plant
Name=SP$00001-LOW  Value=
Name=%_SP$00001_%_APP_%-TO_TEXT  Value=to
Name=SP$00001-HIGH  Value=
Name=%_SP$00001_%_APP_%-VALU_PUSH  Value=
...

FYI, to get the field name of a data element that currently has focus, as ScriptMan suggests, you can use the script below.

Sub GetSAPObjectID()
    '-Variables---------------------------------------------------------
      Dim SAPGUI 
      Dim App 
      Dim Con 
      Dim Ses 
      Dim ActID 

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

    Set App = SAPGUI.GetScriptingEngine()
    If Not IsObject(App) Then
      SAPGUI = Nothing
      Exit Sub
    End If

    Set Con = App.Children(0)
    If Not IsObject(Con) Then
      Set App = Nothing
      Set SAPGUI = Nothing
      Exit Sub
    End If

    Set Ses = Con.Sessions(0)
    If Not IsObject(Ses) Then
      Set Con = Nothing
      Set App = Nothing
      Set SAPGUI = Nothing
      Exit Sub
    End If
    ActID = Ses.ActiveWindow.SystemFocus.ID
    Debug.Print ActID

    Set Ses = Nothing
    Set Con = Nothing
    Set App = Nothing
    Set SAPGUI = Nothing
End Sub

Upvotes: 1

ScriptMan
ScriptMan

Reputation: 1625

I would use the SAP GUI script recorder. Then you can see the recorded script.

Upvotes: 0

Related Questions