cancel
Showing results for 
Search instead for 
Did you mean: 

How to define filter and layout for MB56 result

Former Member
0 Kudos


Hi Gurus,

I just started to work with scripts, so please do not judge me.

Could you please help me to define filter for SAP T_code MB56 result in order to filter result for only specific materials, which are in  "component_list.txt" on C:drive. Also is there way to describe layout for MB56 result?

There is code, but it works in one SAP instance, and doesn't work in another.

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").setCurrentCell -1,"MATNR"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectColumn "MATNR"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_FILTER"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectContextMenuItem "&FILTER"
session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/btn%_%%DYN001_%_APP_%-VALU_PUSH").PRESS
session.findById("wnd[2]/tbar[0]/btn[23]").press


session.findById("wnd[3]/usr/ctxtDY_PATH").text = "C:/"
session.findById("wnd[3]/usr/ctxtDY_FILENAME").text = "component_LIST.txt"   --------------> sap stops here and wait for me to choose file name
session.findById("wnd[3]/usr/ctxtDY_FILENAME").caretPosition = 18
session.findById("wnd[3]/tbar[0]/btn[0]").press
session.findById("wnd[2]/tbar[0]/btn[8]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press

THANK YOU!!!

Elena

Accepted Solutions (1)

Accepted Solutions (1)

script_man
Active Contributor
0 Kudos

Hi Elena,

unfortunately I do not have access to the TA MB56. But you can try the following:

. . .

session.findById("wnd[3]/usr/ctxtDY_PATH").text = "C:/"

session.findById("wnd[3]/usr/ctxtDY_FILENAME").text = "component_LIST.txt"   --------------> sap stops here and wait for me to choose file name

'The following command is unnecessary.

'session.findById("wnd[3]/usr/ctxtDY_FILENAME").caretPosition = 18


'The following command should be replaced.

'session.findById("wnd[3]/tbar[0]/btn[0]").press

session.findById("wnd[3]/tbar[0]/btn[11]").press


session.findById("wnd[2]/tbar[0]/btn[8]").press

session.findById("wnd[1]/tbar[0]/btn[0]").press

. . .


Regards,

ScriptMan

Former Member
0 Kudos

Thank you ScriptMan.

I changed code as you advised , but it still stops at the same place. Please help! and one more question how to define Layout name for Mb56 result as it can't be default layout. Thanks again for looking to this! Cheers, elena

Set args = WScript.Arguments
V_FileName = args.Item(0)
V_Plant = args.Item(1)
V_Material = args.Item(2)
V_Batch = args.Item(3)

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]").resizeWorkingPane 134,24,false
session.findById("wnd[0]/tbar[0]/okcd").text = "/nMB56"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/chkPA_TRFPO").selected = false
session.findById("wnd[0]/usr/chkPA_ALCOA").selected = false
session.findById("wnd[0]/usr/chkPA_CHLAL").selected = false
session.findById("wnd[0]/usr/chkPA_KZNUL").selected = false
session.findById("wnd[0]/usr/chkPA_ALBTC").selected = true
session.findById("wnd[0]/usr/radPA_LIST").select
session.findById("wnd[0]/usr/ctxtPA_MATNR").text = V_Material
session.findById("wnd[0]/usr/ctxtPA_WERKS").text = V_Plant
session.findById("wnd[0]/usr/ctxtPA_CHARG").text = V_Batch
session.findById("wnd[0]/usr/radPA_LIST").setFocus
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").setCurrentCell -1,"MATNR"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectColumn "MATNR"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_FILTER"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectContextMenuItem "&FILTER"
session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/btn%_%%DYN001_%_APP_%-VALU_PUSH").PRESS
session.findById("wnd[2]/tbar[0]/btn[23]").press


session.findById("wnd[3]/usr/ctxtDY_PATH").text = "C:/RALF/"   ' !!!!!!!!!!  still stops here , I can see my "component_LIST.txt" , but need to choose it
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
session.findById("wnd[3]/usr/ctxtDY_FILENAME").text = "component_LIST.txt"
session.findById("wnd[3]/tbar[0]/btn[0]").press
session.findById("wnd[3]/tbar[0]/btn[11]").press
session.findById("wnd[2]/tbar[0]/btn[8]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
Session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectContextMenuItem "&PC"
Session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").Select
Session.findById("wnd[1]/tbar[0]/btn[0]").press
Session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:/RALF/"
Session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "component_batches.txt"
Session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 17
Session.findById("wnd[1]/tbar[0]/btn[11]").press


set objExcel = getobject(,"Excel.Application")
objExcel.visible = true
Set objWorkbook = objExcel.Workbooks(V_FileName)
objWorkBook.Activate
objExcel.Run "SAP_ReturnMB56"

Former Member
0 Kudos

I recorded the file export steps from MB56, then it cleaned up a little and added comments

'click export button

Session.FindById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").PressToolbarContextButton "&MB_EXPORT"

'select local file from export dropdown menu

Session.FindById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").SelectContextMenuItem "&PC"

'select first radio button (change to [1,0] if you want the second radio button "spreadsheet")

Session.FindById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").Select

Session.FindById("wnd[1]/tbar[0]/btn[0]").press

'fill in the path name

Session.FindById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\chorihan\Desktop\"

'fill in the file name

Session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = "test.txt"

'click generate button

Session.FindById("wnd[1]/tbar[0]/btn[0]").press

Looks like you have that same code, but also these lines, ahead of the good code, that look like they need to be removed.

session.findById("wnd[3]/usr/ctxtDY_PATH").text = "C:/RALF/"   ' !!!!!!!!!!  still stops here , I can see my "component_LIST.txt" , but need to choose it

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

session.findById("wnd[3]/usr/ctxtDY_FILENAME").text = "component_LIST.txt"

session.findById("wnd[3]/tbar[0]/btn[0]").press

session.findById("wnd[3]/tbar[0]/btn[11]").press

session.findById("wnd[2]/tbar[0]/btn[8]").press

session.findById("wnd[1]/tbar[0]/btn[0]").press

Chad

Former Member
0 Kudos

Hi Chad,

thank you for trying to help , for code and special thanks for your comments, I think I started to understand better the process with your comments.

Re:"Looks like you have that same code, but also these lines, ahead of the good code, that look like they need to be removed."

Just before to export data from MB56 I need to filter the result by material and the list of these materials located in component.txt. It is why I need these lines above. And code is working in one SAP system (one country), but doesn't work in another SAP (another country). If you can check this scenario, it will be great. Thank you again, Elena

Former Member
0 Kudos

I can see the lines for starting the filtering in the script you posted but then you have file path and name lines interjected in there.

The code to enter the filter criteria should more look like this (in this one, I selected Material from the list of possible filter fields and entered a filter value of 1091)

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_FILTER"

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectContextMenuItem "&FILTER"

session.findById("wnd[1]/usr/subSUB_DYN0500:SAPLSKBH:0600/cntlCONTAINER1_FILT/shellcont/shell").currentCellRow = 2

session.findById("wnd[1]/usr/subSUB_DYN0500:SAPLSKBH:0600/cntlCONTAINER1_FILT/shellcont/shell").selectedRows = "2"

session.findById("wnd[1]/usr/subSUB_DYN0500:SAPLSKBH:0600/btnAPP_WL_SING").press

session.findById("wnd[1]/usr/subSUB_DYN0500:SAPLSKBH:0600/btn600_BUTTON").press

session.findById("wnd[2]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").text = "1091"

session.findById("wnd[2]").sendVKey 0

--your script

Set args = WScript.Arguments

V_FileName = args.Item(0)

V_Plant = args.Item(1)

V_Material = args.Item(2)

V_Batch = args.Item(3)

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]").resizeWorkingPane 134,24,false

session.findById("wnd[0]/tbar[0]/okcd").text = "/nMB56"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/chkPA_TRFPO").selected = false

session.findById("wnd[0]/usr/chkPA_ALCOA").selected = false

session.findById("wnd[0]/usr/chkPA_CHLAL").selected = false

session.findById("wnd[0]/usr/chkPA_KZNUL").selected = false

session.findById("wnd[0]/usr/chkPA_ALBTC").selected = true

session.findById("wnd[0]/usr/radPA_LIST").select

session.findById("wnd[0]/usr/ctxtPA_MATNR").text = V_Material

session.findById("wnd[0]/usr/ctxtPA_WERKS").text = V_Plant

session.findById("wnd[0]/usr/ctxtPA_CHARG").text = V_Batch

session.findById("wnd[0]/usr/radPA_LIST").setFocus

session.findById("wnd[0]/tbar[1]/btn[8]").press

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").setCurrentCell -1,"MATNR"

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectColumn "MATNR"

-----------------------------------------------

Starts setting the filter

------------------------------------------------

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_FILTER"

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectContextMenuItem "&FILTER"

session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/btn%_%%DYN001_%_APP_%-VALU_PUSH").PRESS

session.findById("wnd[2]/tbar[0]/btn[23]").press

-----------------

These highlighted lines definitely don’t belong here

-------------------

session.findById("wnd[3]/usr/ctxtDY_PATH").text = "C:/RALF/"   ' !!!!!!!!!!  still stops here , I can see my "component_LIST.txt" , but need to choose it

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

session.findById("wnd[3]/usr/ctxtDY_FILENAME").text = "component_LIST.txt"

------------

these next 4 lines look suspicious but the filtering process pops-up a couple of windows, so it could match what you did to complete the filtering

-------------

session.findById("wnd[3]/tbar[0]/btn[0]").press

session.findById("wnd[3]/tbar[0]/btn[11]").press

session.findById("wnd[2]/tbar[0]/btn[8]").press

session.findById("wnd[1]/tbar[0]/btn[0]").press

-----------------------------------------------

Starts the export process

------------------------------------------------

Session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectContextMenuItem "&PC"

Session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").Select

Session.findById("wnd[1]/tbar[0]/btn[0]").press

Session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:/RALF/"

Session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "component_batches.txt"

Session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 17

Session.findById("wnd[1]/tbar[0]/btn[11]").press

script_man
Active Contributor
0 Kudos

Hi Elena,

one should the first question clarify first. The other issues must be clarified later.

What you may not know, but unfortunately this is true, the SAP GUI Script Recorder can record any Windows dialogs. Here you have to look for workaround. And it might look like this:

Instead of the switch import from text file (session.findById("wnd[2]/tbar[0]/btn[23]").press)

we use the switch uploading from clipboard (session.findById("wnd[2]/tbar[0]/btn[24]").press). The only thing you still have to solve is the reading of a text file and filling the Windows clipboard.

The modified program code could look like this:

Set args = WScript.Arguments

V_FileName = args.Item(0)
V_Plant = args.Item(1)
V_Material = args.Item(2)
V_Batch = args.Item(3)

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]").resizeWorkingPane 134,24,false
session.findById("wnd[0]/tbar[0]/okcd").text = "/nMB56"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/chkPA_TRFPO").selected = false
session.findById("wnd[0]/usr/chkPA_ALCOA").selected = false
session.findById("wnd[0]/usr/chkPA_CHLAL").selected = false
session.findById("wnd[0]/usr/chkPA_KZNUL").selected = false
session.findById("wnd[0]/usr/chkPA_ALBTC").selected = true
session.findById("wnd[0]/usr/radPA_LIST").select
session.findById("wnd[0]/usr/ctxtPA_MATNR").text = V_Material
session.findById("wnd[0]/usr/ctxtPA_WERKS").text = V_Plant
session.findById("wnd[0]/usr/ctxtPA_CHARG").text = V_Batch
session.findById("wnd[0]/usr/radPA_LIST").setFocus
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").setCurrentCell -1,"MATNR"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectColumn "MATNR"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_FILTER"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectContextMenuItem "&FILTER"
session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/btn%_%%DYN001_%_APP_%-VALU_PUSH").PRESS

Set fso = CreateObject("Scripting.FileSystemObject")

set myfile = fso.opentextfile("C:\RALF\component_LIST.txt")

myClip = myfile.readall

myfile.close

Set WshShell = CreateObject("WScript.Shell")

Set oExec = WshShell.Exec("clip")

Set oIn = oExec.stdIn

oIn.Write myClip

oIn.Close

Do While oExec.Status = 0

    WScript.Sleep 100

Loop

session.findById("wnd[2]/tbar[0]/btn[24]").press

session.findById("wnd[2]/tbar[0]/btn[8]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press

Session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectContextMenuItem "&PC"
Session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").Select
Session.findById("wnd[1]/tbar[0]/btn[0]").press
Session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:/RALF/"
Session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "component_batches.txt"
'Session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 17
Session.findById("wnd[1]/tbar[0]/btn[11]").press


set objExcel = getobject(,"Excel.Application")
objExcel.visible = true
Set objWorkbook = objExcel.Workbooks(V_FileName)
objWorkBook.Activate
objExcel.Run "SAP_ReturnMB56"

Set oIn = Nothing

Set oExec = Nothing

Regards,

ScriptMan

Former Member
0 Kudos

Dear ScriptMan,

You are just Genius!

This workaround with clipboard is working! THANK YOU! THANK YOU! THANK YOU!

If you know the workaround with Layout, it will be great!

Cheers,

Elena

Former Member
0 Kudos

Thank you Chad. It is good solution, and great learning for me .  I just need to filter not by one material , but by many from the list.  And ScriptMan found a workaround for me, which works!

Thanks,

Elena

script_man
Active Contributor
0 Kudos

Hi Elena,

I am pleased that this workaround also works on your computer. If you explain the question relating to layout a little more, you might find a solution.

Regards,

ScriptMan

Former Member
0 Kudos

Hi ScriptMan,

thanks again!

about layout, MB56 doesn't give an option to choose a layout, so result always comes with global default , which I do not have rights to change to a layout which I need. When I choose a layout during the macro running , I can't find how to define layout's name.

Thank you!

Elena

script_man
Active Contributor
0 Kudos

Hi Elena,

as I said, I unfortunately don't have access to TA MB56. All that follows now is just an example. You must record it directly in MB56 and add a few new lines.

. . .

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_VARIANT"

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectContextMenuItem "&LOAD"

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").currentCellColumn = "VARIANT"

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").contextMenu

session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectContextMenuItem "&FIND"

session.findById("wnd[2]/usr/chkGS_SEARCH-EXACT_WORD").selected = true

session.findById("wnd[2]/usr/chkGS_SEARCH-SHOW_HITS").selected = true

session.findById("wnd[2]/usr/txtGS_SEARCH-VALUE").text = "/myLAYOUT"

session.findById("wnd[2]/usr/chkGS_SEARCH-SHOW_HITS").setFocus

session.findById("wnd[2]/tbar[0]/btn[0]").press

session.findById("wnd[2]/tbar[0]/btn[12]").press

'The following program line must now be made variable.

'session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").selectedRows = "19"

myRow = session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").currentCellRow

session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").selectedRows = myRow

session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").clickCurrentCell

. . .

Record your own script please as follows:

1. Choose layout

2. Click with the right mouse button in the first row in the "Layout" column

3. Choose from contextmenu "Find"

4. Enter layout name like "/myLayout"

5. Choose "Search entire word"

6. Choose "Number of hits"

7. OK button

8. Cancel button

9. Mark found line with the left mouse button

10. Finish

Now you just disable the black line marked above in your script and instead add the following lines at the same place:

myRow = session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").currentCellRow

session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").selectedRows = myRow

It could be that the unlabeled ingredients look different on your system.

Regards,

ScriptMan


Former Member
0 Kudos

Thank you ScriptMan!!!!!!!!!!

everything is working! Again you are the Genius!

Thank you for your help!

Really appreciate your help!

Cheers, Elena

Answers (0)