cancel
Showing results for 
Search instead for 
Did you mean: 

How do I script changing the MIGO_GI carrier screen number in one line using the VBA object model?

mburnett
Explorer
0 Kudos

I want to improve my code by changing the carrier screen number in the MIGO_GI transaction in one line instead of several. Currently, I'm turning off error control using On Error Resume Next and running all of the possibilities until I hit the correct one. I know there's a way to make this change by executing a method that directly accesses the object level variables, but I've not been able to figure it out.

I also want to learn how to streamline this code by basing everything off of the object variable (instead of repeating "Session.findById("wnd[0]..." on every single line of script). Again, I know this can be done but I'm not sure how to structure it.

All of the Session code was copied directly from the SAP GUI script recorder. Any pointers on how to further clean up this output and/or execute steps more efficiently would be greatly appreciated!

Sub RunMIGO_GI(matNum, batch, storageLocation, quantityIssued, unitOfIssue, requestedBy, costCenter, GLAccount, issueDate)
Dim mySession As Object, screenID as string
Session.findById("wnd[0]/tbar[0]/okcd").Text = "/nmigo_gi"
Session.findById("wnd[0]").sendVKey 0

'Set mySession = Session.findById("wnd[0]/usr") 'not used yet...this the object variable I want to figure out how to manipulate
'screenID = mySession.Children(1).ID 'not used yet...this contains information about which screen number is active when the transaction is first executed

'the screen number is "0003" if the header and the item details are showing
'turn off error control and run every screen scenario to force screen 0003 so the code that follows will work (all of it is based on screen 0003)

On Error Resume Next 
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0001/subSUB_HEADER:SAPLMIGO:0102/btnOK_HEADER").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0002/subSUB_HEADER:SAPLMIGO:0102/btnOK_HEADER").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_HEADER:SAPLMIGO:0102/btnOK_HEADER").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0004/subSUB_HEADER:SAPLMIGO:0102/btnOK_HEADER").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0005/subSUB_HEADER:SAPLMIGO:0102/btnOK_HEADER").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_HEADER:SAPLMIGO:0102/btnOK_HEADER").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0007/subSUB_HEADER:SAPLMIGO:0102/btnOK_HEADER").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0008/subSUB_HEADER:SAPLMIGO:0102/btnOK_HEADER").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0009/subSUB_HEADER:SAPLMIGO:0102/btnOK_HEADER").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0001/subSUB_ITEMDETAIL:SAPLMIGO:0302/btnBUTTON_DETAIL").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0002/subSUB_ITEMDETAIL:SAPLMIGO:0302/btnBUTTON_DETAIL").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0302/btnBUTTON_DETAIL").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0004/subSUB_ITEMDETAIL:SAPLMIGO:0302/btnBUTTON_DETAIL").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0005/subSUB_ITEMDETAIL:SAPLMIGO:0302/btnBUTTON_DETAIL").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMDETAIL:SAPLMIGO:0302/btnBUTTON_DETAIL").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0007/subSUB_ITEMDETAIL:SAPLMIGO:0302/btnBUTTON_DETAIL").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0008/subSUB_ITEMDETAIL:SAPLMIGO:0302/btnBUTTON_DETAIL").press
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0009/subSUB_ITEMDETAIL:SAPLMIGO:0302/btnBUTTON_DETAIL").press
On Error GoTo 0

'want to run the above 20 lines (i.e. set the carrier screen to 0003) in one step using the mySession object variable

'After setting to "0003" set drop-downs to "Goods Issue" and "Other"
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_FIRSTLINE:SAPLMIGO:0010/cmbGODYNPRO-ACTION").Key = "A07"
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_FIRSTLINE:SAPLMIGO:0010/cmbGODYNPRO-REFDOC").Key = "R10"

'then fill out fields and post
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_MATERIAL").Select
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_MATERIAL/ssubSUB_TS_GOITEM_MATERIAL:SAPLMIGO:0310/ctxtGOITEM-MAKTX").Text = matNum
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_QUANTITIES").Select
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_QUANTITIES/ssubSUB_TS_GOITEM_QUANTITIES:SAPLMIGO:0315/txtGOITEM-ERFMG").Text = quantityIssued
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_DESTINAT.").Select
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_DESTINAT./ssubSUB_TS_GOITEM_DESTINATION:SAPLMIGO:0325/ctxtGOITEM-NAME1").Text = "3000"
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_DESTINAT./ssubSUB_TS_GOITEM_DESTINATION:SAPLMIGO:0325/ctxtGOITEM-LGOBE").Text = storageLocation
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_DESTINAT./ssubSUB_TS_GOITEM_DESTINATION:SAPLMIGO:0325/txtGOITEM-WEMPF").Text = Left(requestedBy, 12)
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_DESTINAT./ssubSUB_TS_GOITEM_DESTINATION:SAPLMIGO:0325/txtGOITEM-SGTXT").Text = "Goods issue request"
Session.findById("wnd[0]").sendVKey 0
If batch <> "" Then
    Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_BATCH").Select
    Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_BATCH/ssubSUB_TS_GOITEM_BATCH:SAPLMIGO:0335/ctxtGOITEM-CHARG").Text = batch
End If
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_ACCOUNT").Select
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_ACCOUNT/ssubSUB_TS_GOITEM_ACCOUNT:SAPLMIGO:0345/ssubSUB_ACCOUNTINGBLOCK:SAPLKACB:1001/ctxtCOBL-KOSTL").Text = costCenter
Session.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0003/subSUB_ITEMDETAIL:SAPLMIGO:0301/subSUB_DETAIL:SAPLMIGO:0300/tabsTS_GOITEM/tabpOK_GOITEM_ACCOUNT/ssubSUB_TS_GOITEM_ACCOUNT:SAPLMIGO:0345/ctxtGOITEM-KONTO").Text = GLAccount
Session.findById("wnd[0]/tbar[1]/btn[23]").press 'POST
End Sub
0 Kudos

You can get the name of the GuiScrollContainer as follows (after you've entered the MIGO_GR transaction):

'Get name of GuiScrollContainer
GSC = "wnd[0]/usr/ssub" & session.Children(0).Children(5).Children(1).Name


Then you can use the GSC variable to reference the Container and make the Session commands a bit shorter:

'select Goods Receipt
session.findById(GSC & "/subSUB_FIRSTLINE:SAPLMIGO:0010/cmbGODYNPRO-ACTION").Key = "A01"



Accepted Solutions (0)

Answers (0)