excel SAP Gui Scipting GridView代码无法从SAP中提取正确的数据

guz6ccqo  于 2023-06-07  发布在  其他
关注(0)|答案(1)|浏览(334)

因此,我正在开发一个数据提取器,以便使用SAP GUI Scripting + VBA(Excel)提取SAP数据。
该脚本将运行一个事务并将数据提取到Excel中。下面是从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

它正确复制了大部分数据,但是SAP中的最后一行没有正确复制。

yc0p9oo0

yc0p9oo01#

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

相关问题