0

I'm currently creating an automation script where data from Excel will be searched in SAP GUI ALV List. I will be looping to the rows that, if it will match anything in the columns "Assignment", "DocumentNo" and "Quantity" to the "textToFind" in Excel, then I will be able to edit the text for each item matched:

SAP ABAP List of G/L account line item display

How will I set the table and loop through the rows of the table until I find the text that I'm looking for?

I believe it will also only allow me to search for the visible rows.

I tried to record the steps in SAP GUI but it only gives me this if I position my cursor somewhere in the column "Assignment":

session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/lbl[18,15]").setFocus
session.findById("wnd[0]/usr/lbl[18,15]").caretPosition = 10

Which I know that it tells me the current cell address (column 18, row 15).

When I tried to check the table name on "Assignment" field (F1), it gives me the name of "RFPOSXEXT".

screen field technical information

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Sevpoint
  • 213
  • 1
  • 8
  • 26
  • Where will you execute your code? Is this a SAP Screen Personas solution? – Eralper Feb 13 '18 at 08:30
  • Basically I will execute the code inside excel vba. I'm not sure regarding SAP screen personas. What I need to do is to search the items in my excel file to a SAP table. – Sevpoint Feb 13 '18 at 08:39
  • You are aware that RFPOSXEXT is not a table! I also do not see that you are searching anything according to the code you provided. You also do not mention the transaction you use in order to display the general ledger accounts. Maybe this helps https://stackoverflow.com/questions/19452461/vba-pulling-data-from-sap-for-dummies – Storax Feb 13 '18 at 09:38
  • I'm new to scripting and I'm not sure if I have got the right table name. But when clicking the "F1" to get property, it says table name is RFPOSXEXT. The transaction that I use is fbl3n which I need to display G/L accounts. – Sevpoint Feb 13 '18 at 10:57
  • Again, RFPOSEXT is a structure regardless what you think. Look at the "table category" and display RFPOSEXT in SE11. Do you know that you can switch the view of FBL3N or FBL4N to ALV grid mode and that you can download the data into an excel file? Maybe that already solves your issue. Otherwise it is a request for programming the complete sapgui script for you. – Storax Feb 13 '18 at 11:00
  • Sorry as I am not familiar with the table. I will check it by tommorow in SAP. So when I switch FBL3N to gridview, I will be able to loop throught each row of the items displayed? As I will not download the data. Rather I will create a script. That for items in excel, I will search it in the items displayed as shown in screenshot. Thanks for your input, I'm beginning to have a clear view. – Sevpoint Feb 13 '18 at 11:17

2 Answers2

3

Let's assume you display the data in a ALV Grid and you have the session ready as you write in your post. Then the following code will copy the data from SAP into excel. You have to adjust the code according to your needs

    Dim wks As Worksheet
    Set wks = " your worksheet here ..."

    Dim Table As Object
    Dim cols As Long
    Dim rows As Long
    Dim i As Long, j As Long

    Set Table = Session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell")

    rows = Table.RowCount - 1
    cols = Table.ColumnCount - 1

    Dim columns As Object
    Set columns = Table.ColumnOrder


    Dim arrCol() As Variant
    ReDim arrCol(cols)
    For j = 0 To cols
        arrCol(j) = (CStr(columns(j)))
    Next
    With wks
        .Range(.Cells(1, 1), .Cells(1, cols + 1)).Value = arrCol()
    End With

    For i = 0 To rows
        For j = 0 To cols
            arrCol(j) = Table.GetCellValue(i, CStr(columns(j)))                
        Next

        With wks
            .Range(.Cells(i + 2, 1), .Cells(i + 2, cols + 1)).Value = arrCol()
        End With

        If i Mod 10 = 0 Then
            Table.SetCurrentCell i, CStr(columns(0))
            DoEvents
        End If
    Next

End Sub

The above code will fail if you don't use griv view control. "Session" must be a valid SAP Guisession pointing to FBL3N with the grid view open. In the link I provided above you will see hot to do that.

Storax
  • 11,158
  • 3
  • 16
  • 33
  • Thank you @Storax really appreciate your help. I will mark this as an answer as you explained how I can loop through each row of displayed items in SAP by using the grid view which is my main question. One last question, will this be as well loop until at the end of the row of Grid? Say I have 400+ items. Thank you once again! – Sevpoint Feb 13 '18 at 13:32
  • Yes, it will loop through the whole table, as rows = Table.RowCount - 1. And Table ist the "pointer" to the grid view. With Table.SetCurrentCell i, CStr(columns(0)) iI take care that it will scroll down. – Storax Feb 13 '18 at 13:37
  • Thanks alot @Storax! I will adjust the code according to my need. This is a big help for me. :) – Sevpoint Feb 13 '18 at 13:41
0

The screenshot shows an ALV (Grid View) but which is displayed via the "ABAP List" technology. It's not to be confused with the GuiGridView object.

A text in an ABAP List has no "table" or "field" name, whatever it's for representing an ALV or anything else (F1 is helpless). Only the column and row numbers can be used to get the texts or to move the cursor on these texts.

SAP GUI Scripting represents the texts from a screen of type ABAP List as a collection of contiguous GuiLabel, GuiTextField or GuiCheckBox, in the property Children of the GuiUserArea object.

You can loop at them by using this script, which shows all the fields in an ABAP List, and which is taken from this other answer about ABAP List (see that answer for more information):

text = ""
For Each field In session.findById("wnd[0]/usr").Children
    text = text & field.CharTop & " " & field.CharLeft & " " & field.Text & " " _
                & field.Id & " " & field.Type & chr(10)
Next
msgbox text

NB: here, the ABAP List is in the "window 0".

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48