
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.
⚠️ Only automate what you are allowed to do manually, and follow your company’s IT policy. Scripts run with your user’s authorisations.
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.