我发现直接引用您想要从中获取信息的单元格更容易。
MyGrid = "wnd[0]/usr/cntlGRID1/shellcont/shell"
iRows = session.findById(MyGrid).RowCount()
Do While i < iRows ' i default value of 0
session.findById(MyGrid).firstVisibleRow = i 'This is an auto scroll to keep info loading with more then 100 rows
SAP_Info = session.findByID(MyGrid).getcellValue(i,"REFBN") 'SAP_Info as a variable to store the data rather than using copy/paste
i=i+1
Cells(i, 1).Value = SAP_Info 'set to excel active work sheet cells in column A
Loop
如果您想提取整行,我建议您使用数组并设置为表格。这是我获取人力确认信息的报告。
Sub Con_Report()
Dim ArrCells() As Variant
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApp = SapGuiAuto.GetScriptingEngine
Set SAPCon = SAPApp.Children(0)
Set session = SAPCon.Children(0)
MyGrid = "wnd[0]/usr/cntlGRID1/shellcont/shell"
hRows = session.findById(MyGrid).RowCount()
ReDim ArrCells(1 To hRows, 1 To 9)
session.findById(MyGrid).firstVisibleRow = 0
Do While h < hRows 'Fill excel sheet
session.findById(MyGrid).firstVisibleRow = h
ArrCells(h + 1, 1) = session.findById(MyGrid).getcellValue(h, "INGPR")
ArrCells(h + 1, 2) = session.findById(MyGrid).getcellValue(h, "AUART")
ArrCells(h + 1, 3) = session.findById(MyGrid).getcellValue(h, "AUFNR")
ArrCells(h + 1, 4) = session.findById(MyGrid).getcellValue(h, "ZORDDESC")
ArrCells(h + 1, 5) = session.findById(MyGrid).getcellValue(h, "TPLNR")
ArrCells(h + 1, 6) = session.findById(MyGrid).getcellValue(h, "IARBPL")
ArrCells(h + 1, 7) = session.findById(MyGrid).getcellValue(h, "NAME")
ArrCells(h + 1, 8) = session.findById(MyGrid).getcellValue(h, "ISDD")
ArrCells(h + 1, 9) = session.findById(MyGrid).getcellValue(h, "ISMNW")
h = h + 1
Loop
Range("B3").Resize(UBound(ArrCells, 1), UBound(ArrCells, 2)).Value = ArrCells
End Sub