SAP GUI脚本运行报表有时返回0条记录,求条件判断处理方案
Great question! When automating SAP report exports, checking for empty results before attempting to save is a critical step to avoid errors or unnecessary files. Here are two reliable methods to implement this with VBScript's If/Then/Else logic, tailored to common SAP report interfaces:
Method 1: Check the Status Bar Message
Most SAP reports will display a clear status message when no records match the selection criteria (e.g., "没有找到符合选择条件的数据" in Chinese, or "No data found for the selection criteria" in English). You can capture this text and use it to trigger your conditional logic.
Example Code:
' After running your report, capture the status bar text Dim statusBarText statusBarText = session.findById("wnd[0]/sbar").text ' Adjust the match string to match your SAP system's language If InStr(statusBarText, "没有找到符合选择条件的数据") = 0 Then ' Records exist: proceed with save session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[0]").select ' Update to your actual export menu path session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "SAP_ALL.TXT" session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 11 session.findById("wnd[1]/tbar[0]/btn[0]").press ' Confirm save Else ' No records: skip save (add logging if needed) WScript.Echo "Report returned no data - skipping save." End If
Method 2: Check ALV Grid Row Count
If your report uses an ALV (ABAP List Viewer) grid, you can directly check the number of rows in the grid. This is more precise than relying on status messages, especially if the status text varies.
Example Code:
' Locate the ALV grid control (update the ID to match your report's grid) Dim alvGrid, rowCount Set alvGrid = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell") rowCount = alvGrid.rowCount If rowCount > 0 Then ' Records exist: execute save workflow session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[0]").select session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "SAP_ALL.TXT" session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 11 session.findById("wnd[1]/tbar[0]/btn[0]").press Else ' No rows found: skip save WScript.Echo "ALV grid has no records - skipping save." End If
Key Notes for Implementation:
- Adjust Control IDs: Always verify the exact IDs of your SAP elements (like the ALV grid or export menu) using the SAP GUI Script Recorder—these can vary between transactions.
- Language Compatibility: If your SAP system uses English or another language, update the status bar match string to the corresponding message.
- Reusable Logic: For your 7-report workflow, wrap the data check in a reusable function (e.g.,
Function HasReportData(session)) to avoid repeating code.
内容的提问来源于stack exchange,提问作者Harvey

