如何使用VBA对象模型编写脚本来一行更改MIGO_GI载体屏幕号?

时间:2019-01-22 15:03:02

标签: excel vba sap sap-gui

我想通过一行而不是多行更改MIGO_GI SAP事务中的载体屏幕号来改进代码。目前,我正在使用On Error Resume Next关闭错误控制,并运行所有可能性,直到找到正确的可能性为止。我知道有一种方法可以通过执行直接访问对象级变量的方法来进行此更改,但是我无法弄清楚。

我还想学习如何通过基于对象变量的所有内容来简化此代码(而不是在脚本的每一行中重复“ Session.findById(“ wnd [0] ...”))。知道可以做到这一点,但我不确定如何构建它。

所有Session代码都是直接从SAP GUI脚本记录器复制的。任何关于如何进一步清理此输出和/或更有效地执行步骤的指示,将不胜感激!

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 个答案:

没有答案