Recording the Sap Macro

Recording the Sap Macro

When you are ready to perform the recording,  be sure you know the transaction you want to execute and the data that you can use.  You should also know the screens you plan to visit and the fields that you will be filling in.  You can always update the recording file, but the better your recording, the less errors you will encounter.

The important thing to remember is that GUI script can NOT perform any logic.  it only works for repeating the same steps over and over again.  This means that if you receive an error on some records, but not others, you will need to execute 2 different scripts.  One script will do the non errors, and other script will do the errors.  You can cut down a lot of time if you can segregate the data early.

 

Activate the recorder.  It is encouraged to always start the main screen in SAP.  This prevents weird things that happens if an error occurs during the playback.

 

Press More if you want to control where the file is saved.

 

When you are ready to start, press the record button (Red Dot Button). Then proceed to execute the transaction you wish to record.

 

Be sure to use ‘/N’ in front of the transaction code for opening Transaction Code in same window and ‘/O’ in front of the transaction code for opening Transaction Code in new window.


Enter in the document number and press Operations Button.

 

When I added a new operation, I entered it on the bottom of the screen.  This allows you to avoid the risk of overriding exisitng data.

Press Enter.
Press Components Tab

 

Again, add the data to the bottom of the screen.
Press Enter
Press Save
Sometimes there will be errors.  If errors occur, you need to remember that maybe not Every order you change will have the error.

I encourage you to press the green arrow back on every recording at the end.  This just helps to avoid incorrect loading of data.

Press stop.
Now, go find the file.  Be sure to RIGHT click on it.  If you double click on it, it will attempt to execute the exact same transaction again.
You should see something similar to this:


The parts of this file you care about are the lines that start: session.xxx
Next up, add this information to the excel file.

SAP Macro in Excel

This will depend on your version of excel, but what you need to do is find the macro button:


Be sure to enable macros.

When the macro window opens, be sure to select the correct view on the left hand side of the screen.  Then move to the code section and find the area shown that says “Paste your recorded script below”.
If there is anything currently in this section, be sure to remove it, and replace it with the information from your recording (session.xxx).

Now, I’m going to show you how to point this to your excel document.
session.findById(“wnd[0]/tbar[0]/okcd”).text = “/niw32”

session.findById(“wnd[0]”).sendVKey 0

Change:
session.findById(“wnd[0]/usr/ctxtCAUFVD-AUFNR”).text = “4000000”
To:
session.findById(“wnd[0]/usr/ctxtCAUFVD-AUFNR”).text = A

session.findById(“wnd[0]/tbar[1]/btn[17]”).press

Change:
session.findById(“wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpVGUE/ssubSUB_AUFTRAG:SAPLCOVG:3010/tblSAPLCOVGTCTRL_3010/txtAFVGD-LTXA1[7,8]”).text = “my test op”
To:
session.findById(“wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpVGUE/ssubSUB_AUFTRAG:SAPLCOVG:3010/tblSAPLCOVGTCTRL_3010/txtAFVGD-LTXA1[7,8]”).text = B

Since this can get long, I’ll jsut show you the first 2 changes.  You’ll notice that I found the first two lines where I entered in data.  I changed it to a single letter (no quotes).  This is the excel column where you enter in the data.  It’s that simple.  If anything is a constant, just leave it as it is.
When you’re done, press save and you’re ready to execute.

Executing the Script

Now, the first step before executing the script is gathering all the data.  Typically, this is done using SE16, SE16N, or certain reports (IW73, VA05, COOIS, etc).  As always, be careful with the data you enter.  It’s your user id that will be assigned to the change :).

Once you have the data you wish to execute, simply go the excel worksheet and execute the macro
*** NOTE:  Remember, when the script is running you can’t do anything with Excel.  You can continue working in SAP, but Excel will be locked for the duration of the script.


**** NOTE ***
This is a VERY important one.  Be sure that the first SAP window open on your screen is the client you wish to change.  If you are doubt, be sure to logout of any client/system you do not want to change.  For example, if you are logged into production and QA.  You want to make the change to QA, I encourage you to log out of production.


Press Run
Note:  if you have multiple excel windows open, be sure the correct macro name is highlighted.  If you aren’t sure, close the other excel documents.

 

Gui Script will always give you a chance to check the results of the FIRST record.

 

I encourage you to always check and make sure it saved properly before pressing ok.  If it did not, press cancel.  Otherwise the script will go all the way to the end.

Once you are done, it will return control back to excel.

 

As long as you see OK in the row, the record processed.  if you don’t see ok, you’ll need to check that record and possibly reprocess it.

Share This:

Enable GUI Script Recording – SAP

Enable GUI Script Recording – SAP

This document describes how you can turn on GUI Script Recording. To use GUI Script Recording with Winshuttle Transaction, the scripting parameter must be set in RZ11 and GUI scripting must be enabled in the SAP GUI. Typically, Basis has to enable the parameter setting and users will have to enable scripting in their SAP GUI.

Enable the RZ11 parameter

  1. In SAP GUI, start transaction RZ11.

  1. Under Param. Name, enter sapgui/user_scripting.

    enter parameter name

  2. Click Change Value.

  3. In the New value box, enter TRUE, and then click the Save button.

    type true click save

    Scripting is enabled.

Enable scripting in SAP GUI

  1. Click Customize Local Layout, and then click Options.

    customize local layout dropdown menu

  2. Click Accessibility & Scripting, and then click Scripting.

    expanded accessibility and scripting section

  3. Check the Enable scripting box.

    enable scripting checkbox

  4. Click OK.

Share This:

Basics of VBA / Macros Chapter – 90 (Userform Part-11)

Interactive Userform

Below we will look at a program in Excel VBA that creates an interactive Userform. The Userform we are going to create looks as follows:

Excel VBA Interactive Userform

Explanation: whenever you enter a value in the ID text box, Excel VBA loads the corresponding record. When you click the Edit / Add button, Excel VBA edits the record on the sheet or adds the record when the ID does not yet exist. The Clear button clears all the text boxes. The Close button closes the Userform. To create this Userform, execute the following steps.

1. Open the Visual Basic Editor. If the Project Explorer is not visible, click View, Project Explorer.

2. Click Insert, Userform. If the Toolbox does not appear automatically, click View, Toolbox. Your screen should be set up as below.

Userform Screen Setup in Excel VBA

3. Add the labels, text boxes (first at the top, the second below the first, and so on) and command buttons. Once this has been completed, the result should be consistent with the picture of the Userform shown earlier. For example, create a text box control by clicking on TextBox from the Toolbox. Next, you can drag a text box on the Userform.

4. You can change the names and the captions of the controls. Names are used in the Excel VBA code. Captions are those that appear on your screen. It is good practice to change the names of the controls, but it is not necessary here because we only have a few controls in this example. To change the caption of the labels, text boxes and command buttons, click View, Properties Window and click on each control.

5. To show the Userform, place a command button on your worksheet and add the following code line:

Private Sub CommandButton1_Click()

UserForm1.Show

End Sub

We are now going to create the Sub UserForm_Initialize. When you use the Show method for the Userform, this sub will automatically be executed.

6. Open the Visual Basic Editor.

7. In the Project Explorer, right click on UserForm1 and then click View Code.

8. Choose Userform from the left drop-down list. Choose Initialize from the right drop-down list.

9. Add the following code line:

Private Sub UserForm_Initialize()

TextBox1.SetFocus

End Sub

This code line sets the focus on the first text box as this is where we want to start when the Userform is loaded. We have now created the first part of the Userform. Although it looks neat already, nothing will happen yet when we enter a value in the ID text box or when we click one of the command buttons.

10. In the Project Explorer, right click on UserForm1 and then click View Code.

11. Choose TextBox1 from the left drop-down list. Choose Change from the right drop-down list.

12. Add the following code line:

Private Sub TextBox1_Change()

GetData

End Sub

13. In the Project Explorer, double click on UserForm1.

14. Double click on the Edit / Add command button.

15. Add the following code line:

Private Sub CommandButton1_Click()

EditAdd

End Sub

16. Double click on the Clear command button.

17. Add the following code line:

Private Sub CommandButton2_Click()

ClearForm

End Sub

These subs call other subs we are going to create in a second.

18. Double click on the Close command button.

19. Add the following code line:

Private Sub CommandButton3_Click()

Unload Me

End Sub

This code line closes the Userform. Time to create the subs. You can go through our Function and Sub chapter to learn more about subs. If you are in a hurry, simply place the following subs into a module (In the Visual Basic Editor, click Insert, Module).

20. First, declare three variables of type Integer and one variable of type Boolean. Declare the variables in the General Declarations section (at the top of the module). This way you only have to declare the variables once and you can use them in multiple subs.

Dim id As Integer, i As Integer, j As Integer, flag As Boolean

21. Add the GetData sub.

Sub GetData()

If IsNumeric(UserForm1.TextBox1.Value) Then
flag = False
i = 0
id = UserForm1.TextBox1.Value

Do While Cells(i + 1, 1).Value <> “”

If Cells(i + 1, 1).Value = id Then
flag = True
For j = 2 To 3
UserForm1.Controls(“TextBox” & j).Value = Cells(i + 1, j).Value
Next j
End If

i = i + 1

Loop

If flag = False Then
For j = 2 To 3
UserForm1.Controls(“TextBox” & j).Value = “”
Next j
End If

Else
ClearForm
End If

End Sub

If the ID text box contains a numeric value, Excel VBA searches for the ID and loads the corresponding record. We use the Controls Collection to easily loop through text boxes. If Excel VBA cannot find the ID (flag is still False), it empties the second and third text box. If the ID text box does not contain a numeric value, Excel VBA calls the ClearForm sub.

22. Add the ClearForm sub.

Sub ClearForm()

For j = 1 To 3
UserForm1.Controls(“TextBox” & j).Value = “”
Next j

End Sub

Excel VBA clears all the text boxes.

23. Add the EditAdd sub.

Sub EditAdd()

Dim emptyRow As Long

If UserForm1.TextBox1.Value <> “” Then
flag = False
i = 0
id = UserForm1.TextBox1.Value
emptyRow = WorksheetFunction.CountA(Range(“A:A”)) + 1

Do While Cells(i + 1, 1).Value <> “”

If Cells(i + 1, 1).Value = id Then
flag = True
For j = 2 To 3
Cells(i + 1, j).Value = UserForm1.Controls(“TextBox” & j).Value
Next j
End If

i = i + 1

Loop

If flag = False Then
For j = 1 To 3
Cells(emptyRow, j).Value = UserForm1.Controls(“TextBox” & j).Value
Next j
End If

End If

End Sub

If the ID text box is not empty, Excel VBA edits the record on the sheet (the opposite of loading a record as we have seen earlier). If Excel VBA cannot find the ID (flag is still False), it adds the record to the next empty row. The variable emptyRow is the first empty row and increases every time a record is added.

24. Exit the Visual Basic Editor, enter the labels shown below into row 1 and test the Userform.

Excel VBA Interactive Userform

Share This: