1

So I am developing a data extractor in order to extract SAP data using SAP Gui Scripting + VBA (Excel).

The script will run a transaction and extract the data to excel. The following is the code to extract the data from gridview:


    'Save gridview
    Set GridView = Session.findById("/app/con[0]/ses[0]/wnd[0]/usr/cntlG_CONTAINER/shellcont/shell/shellcont[1]/shell")

    
' Extract data to excel
    For i = 0 To GridView.RowCount - 1
        For j = 0 To GridView.ColumnCount - 1
            shtInput.Cells(z + i, j + 1) = GridView.GetCellValue(i, GridView.ColumnOrder(j))
        Next j
            shtInput.Cells(z + i, Area) = "Undefined"
    Next i
    Exit Sub

It copies most of the data correctly, however the last rows in SAP isn't copied correctly.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Ossevaolep
  • 23
  • 4
  • Probably that only the first few rows of the grid data is sent to the frontend and you must scroll so that the backend sends the rest of data. – Sandra Rossi Feb 01 '22 at 07:47
  • [This](https://blogs.sap.com/2012/04/02/tip-write-each-table-with-sap-gui-scripting-to-a-csv-file/) might help you in getting an idea how to get all data from an ALV grid – Storax Feb 01 '22 at 10:05
  • It worked, Storax! Thank you! – Ossevaolep Feb 01 '22 at 14:24

1 Answers1

0
Sub Extract_All_From_GRID(grid_id As String, wsName As String)
    Dim ws As Worksheet
    Set ws = Worksheets(wsName)
    ws.Cells.ClearContents

    Dim grid
    Set grid = Session.findById(grid_id)
    
    Dim clmsName() As String
    ReDim clmsName(grid.columncount)
    
    Dim x
    Dim i As Long
    
    grid.SelectAll
    For Each x In grid.SelectedColumns
        i = i + 1
        clmsName(i) = CStr(x)
        ws.Cells(1, i).Value = clmsName(i)
        ws.Cells(2, i).Value = grid.GetDisplayedColumnTitle(clmsName(i))
    Next
        
    Dim r As Long
    Dim c As Long
    For r = 0 To grid.RowCount - 1
        grid.firstVisibleRow = r
        For c = 1 To grid.columncount
            ws.Cells(r + 3, c).Value = grid.GetCellValue(r, clmsName(c))
        Next
    Next r
    
    Set ws = Nothing
    Set grid = Nothing
End Sub
Masa
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '23 at 02:48