Automatically run SAP transactions out of Excel | by Jesko Rehberg | Towards Data Science

Do you frequently have to execute SAP transactions with specific filter parameters and export the result as an Excel file? If so, and if you want to automate these steps using Excel VBA and SAP scripts, this is your story. Out of an Excel file we will automatically run SAP transactions with defined filters/settings, export the results out of SAP and save the result as an Excel file without further manual input. This is very helpful when we have to run specific SAP transactions frequently, always using the same filter/parameter settings. Let’s now start automating this boring repetitive manual work building an Excel-SAP GUI.
In SAP you can record your inputs using the “Script Recording and playback” functionality. This is very similar to e.g. Excel’s macro recorder. To do this in SAP you must press ALT+F12 and then click “Script Recording and playback”:
After you have clicked the red “record” button, everything you will do in SAP will be recorded until you stop the recording via “Stop” button.
In our example we will run the transaction “IW29”, set some filters, and execute the transaction to see the results. We will also export the results out of SAP as an Excel file. After that we will stop the recording and have a look upon the just recorded SAP script file. In our example it looks like this:
We now simply copy and paste this script into our Excel file’s VBA module. Therefore we open our Excel Macro file (e.g. .xlsm) and add below code (Sub Routine) into our module:
Sub SapExport()If Not IsObject(SAPapplication) ThenSet SapGuiAuto = GetObject(“SAPGUI”)Set SAPapplication = SapGuiAuto.GetScriptingEngineEnd IfIf Not IsObject(SAPconnection) ThenSet SAPconnection = SAPapplication.Children(0)End IfIf Not IsObject(SAPsession) ThenSet SAPsession = SAPconnection.Children(0)End IfIf IsObject(WScript) ThenWScript.ConnectObject SAPsession, “on”WScript.ConnectObject Application, “on”End If
This piece of VBA code will set the connection between our Excel file and SAP. Each time we run this code, SAP will show us this message, which needs to be confirmed first before the script can go on:
In case you want to get rid off this “A script is trying to attach to SAP GUI, please confirm via Okay button” message, you have to customize your local layout in SAP as below:
Press (ALT+F12) in SAP to be able to “Customize local layout”. Then click on “Options”, click “Accessibility&Scripting”, and finally choose “Scripting”. Now untick the box ‘Notify when a script attaches to SAP GUI’ and ‘Notify when a script opens a connection’. So in the end the SAP scripting setting should look like this:
Back in your Excel VBA module, you now paste in the recorded session tasks (the transaction’s filters). The only modification you have to do is to replace session with SAPsession, so finally it looks something like this in your Excel module:
SAPsession.findById(“wnd[0]”).maximizeSAPsession.findById(“wnd[0]/tbar[0]/okcd”).Text = “iw29”SAPsession.findById(“wnd[0]”).sendVKey 0SAPsession.findById(“wnd[0]/usr/chkDY_MAB”).Selected = FalseSAPsession.findById(“wnd[0]/usr/ctxtQMART-LOW”).Text = “z2”SAPsession.findById(“wnd[0]/usr/ctxtDATUV”).Text = “10012021”SAPsession.findById(“wnd[0]/usr/ctxtDATUB”).Text = “26012021”SAPsession.findById(“wnd[0]/usr/ctxtDATUB”).SetFocusSAPsession.findById(“wnd[0]/usr/ctxtDATUB”).caretPosition = 8SAPsession.findById(“wnd[0]”).sendVKey 0SAPsession.findById(“wnd[1]/tbar[0]/btn[0]”).pressSAPsession.findById(“wnd[0]/tbar[1]/btn[16]”).pressSAPsession.findById(“wnd[1]/tbar[0]/btn[0]”).pressSAPsession.findById(“wnd[1]/tbar[0]/btn[0]”).pressSAPsession.findById(“wnd[1]/tbar[0]/btn[0]”).press
Until now we have run the SAP transactions with defined filters and exported the results as an Excel file. Finally, we also want to save this file to a defined path. Below you will find an easy solution for this, which is already sufficient enough for our task:
‘Disable the Application Alert before saving the fileApplication.DisplayAlerts = FalseWindows(“FileName”).ActivateActiveWorkbook.SaveAs FileName:=”C:\FileName.xlsx”, FileFormat _:=xlOpenXMLWorkbook, CreateBackup:=False‘Enabling the Application Alerts after saving the fileApplication.DisplayAlerts = True'to “clean up” SAP we return back to SAP’s Easy Access:SAPsession.findById(“wnd[1]/tbar[0]/btn[0]”).pressSAPsession.findById(“wnd[0]/tbar[0]/btn[3]”).pressSAPsession.findById(“wnd[0]/tbar[0]/btn[3]”).pressEnd sub()
If you added all this VBA code into one Sub Routine in Excel you’re done. So in the future you can just start calling your SAP transaction with defined filters out of Excel.
Note: for this VBA code to work properly you must have one instance of SAP open (in other words, you must already be logged in). Please ensure only one SAP mode is active and this one instance must be in start mode:
Big up yourself, you are now able to export result lists out of SAP in an automated way using Excel! You can find the complete Excel Macro on my Github. Many thanks for reading, hope this was supportive! Any questions, please let me know. You can connect with me on LinkedIn or Twitter.
Originally published on my website DAR-Analytics.