Introduction to SAP Gui Scripting
![]() |
As Bill Gates once said: “I will always choose a lazy person to do a difficult job… because, he will find an easy way to do it” |
Scripting can be used to make repetitive time consuming tasks quick and easy. It takes some time and dedication to write a script but in the end your investment can really pay off. Your script can be faster and more accurate than you! So you have to ask yourself: Should I spend a few hours getting a script to work? If you’re only going to do something once then it’s probably not worth the time. However remember two things; when you do write a script for something, you have that script forever and you’re sharpening your scripting skills with every script you write. So go ahead – Think like a lazy person and you might end up more productive!
Starting Out
First you will need a primer on VBScripting – SAP GUI Scripting is written in Microsoft’s VBScript language. This Scripting language is easy to learn is able to handle most simple tasks. It has been included in Windows since Windows 98 so it’s fairly well documented. Microsoft’s VBScript User’s Guide can be found here: [http://msdn.microsoft.com/en-us/library/sx7b3k7y(v=vs.85).aspx]
For more complicated tasks I’ve wrote front ends in Visual Basic and then built and run a VBScript on the fly. Remember the sky is the limit with scripting so if you can imagine it, it can be done.
Of course you’ll also need to know about SAP Gui Scripting. In order for your VBscripts to access the SAP GUI and manipulate data you’ll need to use an API provided by SAP. You can find documentation for the SAP Gui API here: SAP Gui Scripting API [http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/a034a16b-3bfe-2a10-e2bb-8bd880db0b3c]
Once you’ve gathered the proper documentation you can start to develop scripts!
Keep a few things in mind when you write your scripts-
Make it reusable – Think about other possibilities for your scripts. Spending an extra 5 min writing in an extra feature can end up saving you an hour or two down the road when you might need it. Example – I once wrote a script to lock users. Two extra minutes of coding and it also could unlock them. These two minutes saved me an hour of work a few months later.
Do a good job with the comments – I routinely come back to scripts that I wrote one or two years ago. I can’t remember the specifics of what I was working on at the time so good comments help you understand what you were doing at the time. Some programmers say that good comments are more important than the actual code!
Scripting is dangerous! – Everyone makes mistakes. Automation gives you the ability to make mistakes. Many mistakes… very quickly. So be careful! Make sure to test your script on a sandbox before you decide to run it in Production. I cringe whenever someone asks me to run a script in production because it can be very dangerous. So for your own sake, Be Careful! Or at least make sure your Resume is up to date.
And finally – Start small – Do something simple before you take on something crazy, like scripting system refreshes. You must crawl before you can walk young grasshopper!
Setting up your Environment for SAP Gui Scripting
There are two things you’ll need to setup in order to run Gui Scripts. First you’ll need to configure your client, then the system that you are connecting to.
Step 1: Setup your Client
First, you’ll need to setup the SAP Gui Client to allow scripting on your local machine. I’ll be working with SAPGUI 7.30 on Windows.
In the options panel of your GUI, you’ll need to select “Accessibility & Scripting” and then “Scripting” in the left hand column. Then you are presented with three checkboxes.
To start, I would suggest selecting all three check boxes. Here is a description of what they do:
Enable Scripting – This box simply allows scripting on your GUI. (Has nothing to do with the server)
Notify when a script attaches to SAP GUI – Pops up a yes/no box when a script attempts to use the GUI. This is useful to have on because it give you a point to make sure you want to run what you are running. If you don’t have this on, the script will run without any confirmation.
Notify when a script opens a connection – This setting pops up a confirmation when your script connects to a system. I generally turn this off when I run a script that connects to the system multiple times to do what it does. Otherwise I leave this setting on.
Once you’ve checked “enable scripting” you’re ready to run scripts on the client side. Next, you’ll need to check your settings on the server side.
Step 2: Setup your SAP System
You can enable scripting by setting the profile parameter sapgui/user_scripting to TRUE. The value set using this procedure will be lost when the system is restarted. If the administrator edits the application server profile of the SAP System to include sapgui/user_scripting = TRUE, scripting will be enabled by default when the server is restarted.
REMEMBER: ENABLING SCRIPTING IN PRODUCTION SYSTEMS IS GENERALLY A BAD IDEA.
- Start transaction RZ11
- On the Maintain Profile Parameters screen, enter “sapgui/user_scripting.”
- Choose Display.
- In the Display Profile Parameter Attributes screen, choose Change value.
- Enter “TRUE” in the new value field.
- Click the Save Button.
Once you’ve done these two things – You’re ready to run scripts.
I’ll continue to add to this series on SAP GUI Scripting. Stay tuned!
Hello Joe! Excellent post!
Excellent explanation and very good links to expand knowledge.
I appreciate a blog like this.
Thank you very much!
Please keep posting about these topic! 😛
I love your "Bill Gates" quote too, quite true!
Regards,
Oswald
I am trying to execute the SAP GUI from scripts in 'silent mode' which is like running browsers 'headless', so I can perform some simple load tests with running multiple SAP GUI scripts in parallel. Do you know the setting to execute the scripts without the GUI showing on the screen?
Thanks for the article.
Ian
Hi Ian,
Perhaps try posting your query as a new discussion instead of a comment on a blog.
J
nice job Joe 🙂
Great job!!
I´ll be waiting for the following supplies.
Fidel.
Hi
I'm using SAP GUI Scripting for automating SAP GUI 720 from another application. A user marks one or many dokuments id's in one system, klicks a button and the SAP GUI is opened showing a list of documents in SAP DMS.
This is all working fine but the SAP GUI does not look the same when using the GUI Scripting as when opened the normal way. It does not have the correct theme, SAP Signature Theme. It looks like it is using the Classic Theme. When running under Windows 7 the SAP Gui is even worse. Buttons and tabs are invisible.
Is there some way to change so it uses the SAP Signatur Theme or another theme when SAP GUI Scripting is used?
Gunnar
Hi,
Why its bad idea to enable the GUI scripting in production system. can i know the impact?
Regards,
Vardhan
Hi,
The deal is too good to be bad.
It is a way to use standard, or perhaps much smaller cost and zero-risk.
Read some materials on SAP scripting and what SAP recommends realized by the fact that you can run a very long process via scripting, not the fact that the scripting enabled will change performance. Another point is that if it were as problematic by the SAP standard way stops, including 1441550 SAP provides recorders scripting.
Even learned to use the scripting in SDN and SCN but not in the forums, but in the SAP distributes itself.
From what I understand there are also objects of authorizations that may make further restrictions on users as to what will run in scripting.
Below is some information from SAP and notes:
http://scn.sap.com/docs/DOC-8971#section12
http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/80aaac18-2dfe-2a10-bbb1-ec9b3760ea4c?QuickLink=index&overridelayout=true&28385438934341
http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/e00d6fd4-eaf4-2a10-90a5-db931026edc8?QuickLink=index&overridelayout=true&27801323333531
Note 1441550
https://websmp130.sap-ag.de/sap(bD1wdCZjPTAwMQ==)/bc/bsp/spn/sapnotes/index2.htm?numm=1441550
Hi,
The deal is too good to be bad.
It is a way to use standard, or perhaps much smaller cost and zero-risk.
Read some materials on SAP scripting and what SAP recommends realized by the fact that you can run a very long process via scripting, not the fact that the scripting enabled will change performance. Another point is that if it were as problematic by the SAP standard way stops, including 1441550 SAP provides recorders scripting.
Even learned to use the scripting in SDN and SCN but not in the forums, but in the SAP distributes itself.
From what I understand there are also objects of authorizations that may make further restrictions on users as to what will run in scripting.
Below is some information from SAP and notes:
http://scn.sap.com/docs/DOC-8971#section12
http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/80aaac18-2dfe-2a10-bbb1-ec9b3760ea4c?QuickLink=index&overridelayout=true&28385438934341
http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/e00d6fd4-eaf4-2a10-90a5-db931026edc8?QuickLink=index&overridelayout=true&27801323333531
Note 1441550
https://websmp130.sap-ag.de/sap(bD1wdCZjPTAwMQ==)/bc/bsp/spn/sapnotes/index2.htm?numm=1441550
Very nice post. Thank you.
I was wondering if is there a solution for those new SAP web interfaces, like the one used in some transactions in Solution Manager, or the Fiori used in SAP HANA.
I was very happy with VBScripts and suddenly I couldn't use them for some transactions in SolMan.
help me with below : i can't next pass each items to input line " text "
Sub Macro7()
Set SapGuiAuto = GetObject("SAPGUI")
If Not IsObject(SapGuiAuto) Then
Exit Sub
End If
Set App = SapGuiAuto.GetScriptingEngine
If Not IsObject(App) Then
Exit Sub
End If
Set Connection = App.Children(0)
If Not IsObject(Connection) Then
Exit Sub
End If
Set session = Connection.Children(0)
If Not IsObject(session) Then
Exit Sub
End If
Dim i As Integer
i = 0
While Cells(10 + i, 1).Value <> ""
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nmb02"
session.findById("wnd[0]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/ctxtRM07M-MBLNR").Text = Range("G9").Value
session.findById("wnd[0]/usr/txtRM07M-MJAHR").Text = Range("G10").Value
session.findById("wnd[0]/usr/txtRM07M-MJAHR").SetFocus
session.findById("wnd[0]/usr/txtRM07M-MJAHR").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[20]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/txtMSEG-SGTXT").Text = Cells(10 + i, 1).Value
session.findById("wnd[0]/usr/txtMSEG-SGTXT").SetFocus
session.findById("wnd[0]/usr/txtMSEG-SGTXT").caretPosition = 16
session.findById("wnd[0]").sendVKey 19
i = i + 1
Wend
End Sub
Dear All,
I have a just question below,  ( script to excel )
i used SAP Script to recode for my work, but i have a problem , exp : i want to input box " other informaiton " is "VN-..." for frist ( before input , it is empty ) , below is after i input it, i want ask next time i run script in this matertial ,plant,batch and input ox " other informaiton " is "VN-..." but is was there before , so i want it to notify that "error" in msgbox
session.findById("wnd[0]/usr/subSUBSCR_BATCH_MASTER:SAPLCHRG:1111/subSUBSCR_HEADER:SAPLCHRG:1500/ctxtDFBATCH-MATNR").Text = Range("A10").Value   'Range("A10").Value="VN-20140-16"
End Sub