Blog
Sub ExtractIDocSegmentFieldToExcel()
Dim SapGuiAuto, SAPApp, SAPCon, SAPSession
Dim ExcelSheet As Worksheet
Dim Grid, RowCount, i, resultVal
Set SapGuiAuto = GetObject(“SAPGUI”)
Set SAPApp = SapGuiAuto.GetScriptingEngine
Set SAPCon = SAPApp.Children(0)
Set SAPSession = SAPCon.Children(0)
Set ExcelSheet = ThisWorkbook.Sheets(1)
‘Reference the SAP grid control; change the path to your specific control
Set Grid = SAPSession.findById(“wnd[0]/usr/cntlGRID1/shellcont/shell”)
RowCount = Grid.RowCount – 1 ‘zero based
For i = 0 To RowCount
‘1. Select and double-click the row to open the IDoc
Grid.selectedRows = i
Grid.doubleClickCurrentCell
SAPSession.findById(“wnd[0]”).maximize
‘2. Open the first segment (might be a tree, tab, or button – adjust for your transaction)
SAPSession.findById(“wnd[0]/usr/tblSAP_table_of_segments”).getAbsoluteRow(0).doubleClick
‘3. Get value of the 2nd field in segment (adjust field ID as per your screen)
resultVal = SAPSession.findById(“wnd[0]/usr/subSUBSCREEN1:SAPLIDOC:0500/txtSOME-FIELD2”).Text
‘4. Write value to Excel (row i+2 to skip header)
ExcelSheet.Cells(i + 2, 1).Value = resultVal
‘5. Go “Back” to list (press F3 or click the back button)
SAPSession.findById(“wnd[0]/tbar[0]/btn[3]”).press
SAPSession.findById(“wnd[0]”).maximize
Next i
MsgBox “Process complete!”
End Sub
Get a FREE eBook on How to Prepare for Any Interview
Subscribe
