
π This article includes 2 downloadable practice files β
If you pull the same SAP report every morning β same transaction, same variant, export to Excel β you can hand that job to a macro. SAP GUI Scripting lets VBA drive the SAP GUI exactly as you would.
In this article
1. Make sure scripting is enabled
- Server side: the profile parameter
sapgui/user_scriptingmust beTRUE(your Basis team checks this in RZ11). - Client side: SAP Logon β Options β Accessibility & Scripting β Scripting β tick Enable scripting. You may also untick the notification pop-ups while testing.
2. Record the steps
In SAP GUI open Customize Local Layout (Alt+F12) β Script Recording and Playback, press record, run your report and export it. SAP writes a .vbs file with lines like:
session.findById("wnd[0]/tbar[0]/okcd").text = "/nFBL3N"
session.findById("wnd[0]").sendVKey 0
Each findById path points to one field or button. You will paste these lines into Excel.
3. Connect to the open SAP session from Excel
Function GetSapSession() As Object
Dim SapGui As Object, app As Object, conn As Object
On Error GoTo NoSap
Set SapGui = GetObject("SAPGUI")
Set app = SapGui.GetScriptingEngine
Set conn = app.Children(0) ' first open connection
Set GetSapSession = conn.Children(0) ' first session (window)
Exit Function
NoSap:
MsgBox "Please log on to SAP first.", vbExclamation
End Function
4. Replay the recorded steps
Sub RunDailyExport()
Dim session As Object
Set session = GetSapSession()
If session Is Nothing Then Exit Sub
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nFBL3N"
session.findById("wnd[0]").sendVKey 0
' ...paste your recorded lines here (variant, dates, execute, export)...
' use a cell for dynamic values instead of recorded constants
' session.findById("wnd[0]/usr/ctxtSO_BUDAT-LOW").Text = Format(Sheets("Control").Range("B2").Value, "dd.mm.yyyy")
End Sub
Replace fixed values from the recording (dates, company codes, file names) with cells on a Control sheet. Now anyone can change the parameters without touching the code.
Make it robust
- Check the status bar after each important step:
session.findById("wnd[0]/sbar").Textcontains SAPβs message (e.g. βNo items selectedβ). - Use a saved layout/variant in the report so column order never changes.
- Date format must match your SAP user settings (SU3), otherwise fields reject the value.
- Do not use the machine while the script runs β clicks and focus changes can break it.
Want to understand the basics first? See SAP articles or the older posts on enabling GUI script recording.
Test formulas dynamically in our web formula laboratory or download the companion Excel workbook.
π Practice files for this article
β¬ Download all 2 files (ZIP Β· 1 KB)
- πΎExcel VBA module (.bas)Practice file from the matching course lesson.β¬ BAS Β· 886 B
- πΎLoop and export module (.bas)Practice file from the matching course lesson.β¬ 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.
Atul (AJ) builds office-automation and local-first AI tools as Atulya AI, and works with Excel, SAP and Oracle EPM every day. He writes about the shortcuts, macros and automations that actually save time β plus the occasional wacky fact. Videos on YouTube: Lazy Automator AI (@LazyAutomatorAI).
More posts by Atul βStuck on a step? Ask a question and the AI answers using this article.