SAP Automation


Hi there, welcome back ! 😃

 

In this tutorial we will see how can we automate basic and repetitive task done in SAP through the use of VBA macros so if you're using SAP and do a lot of extract in excel you came to the right place !

 

⚠️⚠️ Important note ⚠️⚠️

To be able to follow along this tutorial you will need to have access to a SAP session where the scripting is enable.

 

=> To check if the scripting capacity is enable on your session click onto the "customize local layout" and select "script recording and playback"

 

 

Normally you should see a window like the one following popping up

 

 

If the running icon in green is greyed or disabled follow the instruction here to enable the scripting option in your session

 

Alright, now that we're correctly set up let's get back to our tutorial. In order to keep things simple we will take the example of an extract operation from SAP to excel in order to give you a feel of how it is done and allow you to then change to your liking this example in order to suits your need but enough talking let's get down to it.

 

The idea here will be to ask SAP to code for us the automation of the task, save it in a file on our local desktop in  order for us to then use it into our vba macro and automate this process by just running our macro in excel. So first go to SAP and open the script recorder as explained above. Once you have the Record and Playback window at your disposal choose the location where you want SAP to save the code script and then click on the  green  arrow to start recording your script.

 

In order for SAP to record our script we will have to perform the extraction process as if we are normally doing it so let's just do that.

 

=> Given that we're not working on the same database the code recorded by SAP on your side will automatically be different than  mine so don't worry if you  don't obtain the same line as I do.

 

Once the process is done and that you have extracted the SAP form in excel format, go back to the script recorder and hit the yellow square to stop the recording. And that's it, we've done the SAP part and now have at our disposal  a GUI script allowing us to automate the SAP process.

 

So let's move now over to excel and open the VBA editor. Here we will use this code template developed initially by Csongor Varga so just copy it from the github page and paste it into your code module as follows :

 

 

Once it's done, it's pretty straightforward we only have to copy and paste the code from our SAP GUI script recorded earlier in our VBA code as follows and replace the prefix session of our SAP GUI script by objSess in order to fit with the requirement of the code template.

 

⚠️⚠️ Important note ⚠️⚠️

 

 In order for the code to work you'll need to enable the Sap Gui package in vba. To do so go into tools in the vba IDE toolbar, there double click on references and check the SAP GUI library in the drop down list.

 

Also don't forget to modify the W_system code accordingly to your SAP session in order to enable the macro to connect to your SAP GUI session.

 

Tip : The W_system code is the first code starting from the left on the bottom right of your SAP GUI window.

 

 

And that's it we now have our at disposal a macro allowing us to automate a form extraction process from SAP ! 😃

 

As always you'll find the full code for this tutorial on the github repo and don't hesitate to play around with the code by trying to automate other tasks with the SAP code recorder as possibilities are truly endless  ^^

 

Happy coding, see you next time ✌️