SAP GUI Scripting Lesson 5: Loops, Errors and Exports to Excel

📎 This article includes 1 downloadable practice file ↓

⏱ 2 min read

📘 SAP GUI Scripting Beginner Course · Lesson 5 of 5

In this article
  1. Loop over inputs
  2. Exports
  3. Check what happened
  4. Where people go wrong
  5. Practice

Real automation runs the same report for several company codes or periods, exports each, and tells you what happened.

Loop over inputs

Put company codes in A2:A10 and loop: for each, set the field, execute, export, and write a status in column B.

Exports

Record the export once (List › Export › Spreadsheet, or the ALV export button), then paste those lines into the loop with the file name built from the company code: "C:\Reports\FBL1N_" & cc & ".xlsx".

Check what happened

msg = session.findById("wnd[0]/sbar").Text          ' status bar
If session.Children.Count > 1 Then                  ' a popup window opened
    session.findById("wnd[1]/tbar[0]/btn[0]").press   ' e.g. confirm (check which button!)
End If
💡 Write a status for every row (Done, No data, error text) back to Excel. When something fails at company code 7, you can re-run just that one.
⚠️ Don’t blindly press buttons on unexpected popups. Log the popup text and stop; an automatic ‘Yes’ on the wrong question can post or delete data.

Where people go wrong

Problem Fix
Export dialog differs between reports Record the export for each report
Excel file already open Close or use unique names

That’s the SAP GUI scripting course. Next: SAP basics.

Use SAP GUI scripting only where your company allows it, on a test/quality system first, and never to bypass authorisations. Examples use standard transactions and made-up data.

Practice

Import the module, list a few company codes and adapt the export lines from your own recording.

📎 Practice files for this article

  • 💾
    Lesson 5 scriptExample script with comments; adjust transaction fields to your system. Test in a non-production client.
    ⬇ BAS · 979 B

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong