If you have ever done some scripting in VB for SAP GUI you some of this may look familiar. There are how ever a few differences. From what I've found these differences are also not well documented, at least anywhere I've found. Thus the reason for this blog.
So in this post as the title implies we'll be covering some of the basic commands for working with the SAP GUI. First you must be connected to use these so if you don't know how to do that see the post
here.
This first one is self explanatory but let's break it down.
session.StartTransaction(Transaction="VL03N")
session is the variable we stored the SAP session connection data in when we connected to the SAP GUI. We'll be using this a lot so depending on the setup for your script you may want to make this a global variable so it doesn't have to be passed to your functions. Later on we'll look at more advanced ways to setup the connection where we put it in its own function and call it from our script.
You don't have to use functions for gui scripting you can just right flat code that executes from top to bottom but functions are very helpful for the same reason as in other programs.
The
StartTransaction is used as you would guess to start a given SAP transaction in the given session. The StartTransaction function takes a single parameter the technical name of the transaction. You can use the "Transaction=" bit or not it is up to you. However the double or single quotas around the technical name are required. Basically is needs a string.
This next one is how you can execute a given command.
session.FindById('wnd[0]/usr/ctxtLIKP-VBELN').text = deliverynumber
This again uses session. The
FindById is used to find a given gui object within the session. The string parameter that is passed is the ID of that gui object. I'll show you in another post how to find those IDs. The
.text is the commend which is preformed on the given gui object. The text command can be used in both directions, which is to say you can either get the text of the gui object. You can also pass text to a gui object that accepts text like the above example.
deliverynumber is just the value stored as a variable which we are passing.
One the VL03N screen we open with the StartTransaction command this would populate the Delivery field with the value passed. Note the value here can be a string or an int.
After you enter a delivery in transaction VL03N you would press enter or click the Enter button on the top toolbar. To do this we use the next command.
session.FindById('wnd[0]/tbar[0]/btn[0]').Press()
The only thing that changed here are the gui object ID and the command we execute on that gui object. As you can guess the Press() command clicks the gui object the same as with a single click of the left mouse button. However unlike keyboard and mouse scripting the mouse never moves so you can continue to use the computer while your script is running. You can even use another session of SAP along side the scripting.
So with this you have the very basics to start using SAP GUI scripting with Python. There is much we haven't covered but we'll get to it in later posts.
A great way to determine the commands needed to preform a given task is to use the VB script recorder built into SAP. It will have to be converted to Python syntax but it should get you started if your first script goes beyond what we have covered here.