How to SAP GUI Scripting
In generally, a script is doing exactly the same what a user would do,but it is automated, so no / less user interaction. However, scripts are executed in foreground and not like LSMW or ECATT in Background. In foreground means, it is not possible via Job rather it is calling transactions via the GUI. Nevertheless, it is possible to run it just on side, so that further tasks can be performed as well in other sessions
SAP GUI scripting allows you to automate the Windows SAP GUI in much the same way as you automate Excel. In fact you can call the SAP GUI directly from an Excel macro. Read up more on it here. The SAP GUI has a macro recording tool much like Excel does. It records macros in VBScript which is nearly identical to Excel VBA and can usually be copied and pasted into an Excel macro directly.
- fast set up
- no ABAP required
- can be used in further systems afterwards, depending on mandatory field
- Development in Testsystem and running in productive possible
- Usage for testing purposes
- Existing authorization of user is considered
SAP recommends to use ECATT only for testing purpose s and no mass update of dataset
Can be used, but the effort for creation is much higher, but it would allow a higher flexibility as in certain cases no transaction is called but functions / BADI direct. Furthermore, the high flexibility is a risk also, whereas with SAP GUI scripting the existing authorization of the user are always checked. He / she can only execute one transaction multiple times.
Set up the personal usage in Options menue. Within the GUI settings the option the scripting has to be acitviated
The notify options will inform the user, when a script starts or is connected to the GUI:
- Enable scripting: The user can enable and disable scripting for their own use if system settings allows it
- Notify when a script attaches to a running GUI: A message appears whenever a script attaches to the SAP GUI
- Notify when a script opens a connection: A message appears whenever a script opens a new GUI connection.
As normally the user is executing by himself, they can be deactivated. When activated the user will be informed that a script is going to start. Unless the user press ok, the script will not start.
Within the system parameter the Gui Scripting need to be activated (RZ11)
- sapgui/user_scripting — needs to be true otherwise scripts can not be executed, (is working with next logon and not immediate after change)
- sapgui/user_scripting_disable_recording — needs to be false, otherwise recording is not possible
- sapgui/user_scripting_force_notification — If this parameter is set to TRUE, a notification is always displayed at the frontend, regardless of the client options
- sapgui/user_scripting_per_user — defines that authorisation checks should be performed. If TRUE authorization object S_SCR is checked
- If not assigned and true, menu point is greyed out
If one of the buttons in scripting submenu is disabled, the relevant parameter is set with the wrong value.
Beside, the settings in SAP it is recommended to include the source “SAP GUI Scripting API” within Excel, especially when the script should be executed with different parameters / values several times.
References can be found in VBA under tools and references. If the reference is not available, it should be added via browse button from the path shown above. This is relevant for running the script later on and doing some VBA programming
3. Create a Script
Create a Script is pretty easy by using the setting script recording and playback.
By pressing it a new sub-screen will open with option to record or execute.
If a button is greyed out or not existing, system parameters have to be changed. After ending the recording, the script will be saved in the path mentioned.
With recording a VBA script is created including all steps (pressing and entering).
Script after changing the region in a vendor
This scriprt could be pasted to a new excel file within the macro editor and executed again. Values could be changed manually within the coding directly also. Nevertheless, manual changes would leed to not big saving in mass processing.
3.2. Preparing Excel — Using VBA
At the end the script created can be combined with VBA to be able to run it multiple times without user interactions. Example coding is available under https://www.youtube.com/watch?v=oPPhA14Pm-8.
The coding is partially explained within the video also.
3.3. Example Excel
For changing asset master records the file GUI_Scripting_Example.xlsm (see below) has been created. Nevertheless, it could be used for further changes / transactions also.
Within your file two sheets are needed:
- First one is used for general Settings
- Second one includes the data to be changed, so key values for which datasets Need to be changed and what should be the new content
3.3.1. Settings sheet
Just the red box need to changed and there only column B. With the help of those figures the relevant session to be used is found:
- System & Client: Content should be placed without separtor and includes the relevant system and client, where changes should be done
- TCode: There the relevant transaction should be mentioned. This one could be used for execution as well as the for getting the correct modus Tcode
- Transaction started: If not empty the transaction mentioned before need to be started within the respective Client, otherwise the script will take the first session found in the System
The further columns are relevant for finding the correct datasets in the Data sheet. There the word “Status” is searched and considered for counting
The Status part on the right hand side is just used for giving more infos on the DATA-sheet related to the Status
3.3.2. DATA sheet
Within the data sheet just the Connection of line 4 and 3 is fixed. There should be no space between those two lines. However, there could be up to 19 before the heading column (within screenshot line 4) and upto 7 columns before the status column. If more are needed, the formulas within the settings table need to be adjusted resp. values set hard.
Furthermore, the value in cell E4 should not be changed as it is referenced. If changed the value in the settings sheet (E5) need to be changed also.
Explanation of columns for data:
- Within the example the values which are relevant for selecting and changing could stay in column A to D.
- Column E will be set by the VBA scripting and gives the user at the end the info if processed or not. At the beginning the status column should be filled with value 0, so the script considers all value as to be processed.
- Column F in examples just references the column before and returns the value from the settings sheet for giving the user more transparent info
- Column G shows the message from the status after processing of the dataset
With pressing the button, the script will be executed.
Within the coding there are some additional function for:
- Main function started by pressing the button, which is processing the lines and creates the logs(StartProcessing)
- reading the settings tab (sub read_settings)
- building up the connection with the settings given (Function Attach_Session) and
- Function ProcessRow which includes the SAP GUI Script itself
The originial script created by SAP will be used in the example:
Just the red marked section need to be pasted into the VBA script. Normally, the entire code created by the recording can pasted within the red marked section from the following screen and there the values to be entered should be replaced with the variables resp. direct cell content
For better visibility the coding is split:
- Calling transaction: At the beginning the transaction from the setting tab is called. However, it is enhanced with a “/N” to start always from the beginning, e.g. when errors occur
- Selection: There the key columns (1 and 2) are read and the transaction is entered with comment “press” at the end
- Data columns: there the data is assigned to the fields whcih schould be changed. In the used case it is just the depreciation key and useful life for years
- Within the breaks of coding line it is visible which column and row is considered to be entered in SAP. As the line two is not enterable in the screen before it is not counted.
- With save of data, the systems gives warnings per line. To have it flexible a loop has been generated.
Furthermore, the script could be used for reading the value by changing the order of the assignment also. Then the value from SAP will be read and copied into the Excel.
4. Start Scripting
- adjusting the values in the settings tab
- entering the script into the VBA coding
- adjusting the script by assigning the variables to the relevant fields
- adjusting the Data sheet by deleting / adding additional columns before the status column
- Entering the values to be changed
The script can be executed by pressing the button. It makes sense to execute it for one data set first and have the relevant session open. For doing so, a break point can be set in the macro and step wised execution can be made. After successful processing, the other data sets can be executed.
Thanks for reading