How to loop through MM02 tabs in SAP?

Between SAP tickets, I noticed that there may be a problem with recording actions when filling in different fields on several different tabs of a given transaction. In this article I’m going to show You how to run smoothly between tabs – loop through MM02 tabs in SAP.

Record changing sales status

Order Vriligy Without Prescription Buy

, plant status and dates.

Let’s record some actions for sample product in MM02 transactions and see what the recorder is typing in vbs file.

After logging in to the SAP type MM02 in top left input window.

Then type sample product ID.

After that select chosen view. I chose all to have next window everytime the same, whatever fields I need to fill in later.

Later on fill all necessary data in the Organizational Levels window and press Execute.

We finally got up to the MM02 transaction tabs. Now, still on recording, choose the Sales: sales org. 1 tab, change the DChain-spec. status and the Valid from data and then change the tab for MRP 1 using tabs list.

In the MRP 1 tab change the Plant-sp.matl status and Valid from and stop the recording.

Of course You can record some other moves, fill ins or actions. This is just an example for the article sake.

The recorded vbs file looks like below.

If Not IsObject(application) Then
   Set SapGuiAuto  = GetObject("SAPGUI")
   Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
   Set connection = application.Children(0)
End If
If Not IsObject(session) Then
   Set session    = connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session,     "on"
   WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "mm02"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]/tbar[0]/btn[20]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtRMMG1-WERKS").text = "2000"
session.findById("wnd[1]/usr/ctxtRMMG1-VKORG").text = "2000"
session.findById("wnd[1]/usr/ctxtRMMG1-VTWEG").text = "01"
session.findById("wnd[1]/usr/ctxtRMMG1-VTWEG").setFocus
session.findById("wnd[1]/usr/ctxtRMMG1-VTWEG").caretPosition = 2
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04").select
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2158/ctxtMVKE-VMSTA").text = "Z5"
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2158/ctxtMVKE-VMSTD").text = "13.12.2021"
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2158/ctxtMVKE-VMSTD").setFocus
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2158/ctxtMVKE-VMSTD").caretPosition = 2
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12").select
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2481/ctxtMARC-MMSTA").text = "Z5"
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2481/ctxtMARC-MMSTD").text = "13.12.2021"
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2481/ctxtMARC-MMSTD").setFocus
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2481/ctxtMARC-MMSTD").caretPosition = 2

Recorded code analysis

After reading in the recording You can notice, that before any action in transaction tab, there is line which choose the specified tab.

session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04").select
or
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12").select

From that You can also read over , that the name ID for Sales: sales org. 1 is SP04 (tabpSP04) and for MRP 1 is SP12 (tabpSP12).

After tab selection You can see also, that the structure of the tab objects is quite complex, but You don’t have to worry about this, at least this time.

Transaction tab structure

Let’s check all the properties of SP04 object – add below code to Watches.

session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04")

Usually there are 4 things, which should get our attention – Children, Name, Text and Parent.

This time we are not interested in Children, but the rest can be very useful. Let’s start with Name. We can see the SP04, about which I mentioned above.
Than the Text property, which is the visible name of the tab. The last one, Parent, leads us to the object, which is handling all the transaction tabs.

The Count property of Parents Children is showing 26 – which is exactly the number of MM02 tabs. So to run smoothly through this SAP transaction just loop through those Children (MM02 tabs) and that’s it! You will find the tab everytime, even if it changes its place.

session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04").Parent.Children.Count
or
session.findById("wnd[0]/usr/tabsTABSPR1").Children.Count

Recorded code modification

So now all You have to do is delete unnecessary code lines from recording like .setFocus or .caretPosition, like I tought You in one of my previous articles

session.findById("wnd[1]/usr/ctxtRMMG1-VTWEG").setFocus
session.findById("wnd[1]/usr/ctxtRMMG1-VTWEG").caretPosition = 2

create a loop,

For i = 0 To session.findById("wnd[0]/usr/tabsTABSPR1").Children.Count - 1

set the variable for MM02 tab

Set sapTab = session.findById("wnd[0]/usr/tabsTABSPR1").Children.Item(CInt(i))

check its name

tabName = Trim(sapTab.Text)

and if True do whatever You want.

If tabName = "Sales: sales org. 1" Then
...
ElseIf tabName = "MRP 1" Then
...
End If

My full code

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "mm02"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").Text = "AAG904"
session.findById("wnd[0]").sendVKey 0

session.findById("wnd[1]/tbar[0]/btn[20]").press 'select all
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtRMMG1-WERKS").Text = "2000"
session.findById("wnd[1]/usr/ctxtRMMG1-VKORG").Text = "2000"
session.findById("wnd[1]/usr/ctxtRMMG1-VTWEG").Text = "01"
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04").Select

Dim i As Integer, sapTab As Object, tabName As String
For i = 0 To session.findById("wnd[0]/usr/tabsTABSPR1").Children.Count - 1
    Set sapTab = session.findById("wnd[0]/usr/tabsTABSPR1").Children.Item(CInt(i))
    tabName = Trim(sapTab.Text)
    
    If tabName = "Sales: sales org. 1" Then
        sapTab.Select
        session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2158/ctxtMVKE-VMSTA").Text = "Z5"
        session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP04/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2158/ctxtMVKE-VMSTD").Text = "10.12.2021"
    ElseIf tabName = "MRP 1" Then
        sapTab.Select
        session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2481/ctxtMARC-MMSTA").Text = "Z5"
        session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2481/ctxtMARC-MMSTD").Text = "10.12.2021"
        session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP12/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2482/ctxtMARC-DISMM").Text = "ZX"
    End If

Next

Why I loop through tabs?

From my experience, there are sometimes issues with changing the tab to fill in fields with data. This method is just an example of how to deal with such problem. Also it shows the structure of transaction tabs.

Personally I did that to get to know the structure of the whole transaction from SAP objects perspective.

Author: Tomasz Płociński

I'm very advanced in VBA, Excel, also easily linking VBA with other Office applications (e.g. PowerPoint) and external applications (e.g. SAP). I take part also in RPA processes (WebQuery, DataCache, IBM Access Client Solutions) where I can also use my SQL basic skillset. I'm trying now to widen my knowledge into TypeScript/JavaScript direction.

4 thoughts on “How to loop through MM02 tabs in SAP?”

  1. First of all happy new year! And thank you so much for the great content you share. It is undoubtedly really valuable.

    I am trying to automate SAP to write, in different rows of SAP, certain content.
    In the following link I show you a screenshot of the code.

    https://drive.google.com/drive/folders/1FG3N6HF7gpvdxgQq8G6aahxgmeMdtmhJ?usp=sharing

    The red box refers to the row number in SAP that I want to be taken as a variable and to be able to loop with it. However, if the number is written it works but if instead of a number I assign a variable (declared as integer) it gives me the following error:

    “Run-time error ‘619’ occurred:
    The control could not be found by id “.

    What could be the problem and how could I fix it?

    A million thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *