问题描述
我刚刚开始编程,并意识到我的编程技能很差.请对我应该做得更好的代码的哪些部分提出一些批评.
I have just started programming and am aware of my poor programming skills. Please provide some critique on which parts of my code I should have done better.
对于我在第一篇文章中不清楚的解释,我深表歉意.我添加了一些编辑来澄清这个问题.
I apologize for unclear explanation in my first post. I added some edits to clarify the matter.
我的问题:
如果接下来继续出错:我把它放在正确的地方了吗?
If error resume next: did I put it in the right place?
有时在运行 SAP 时会出现额外的窗口.我只需要在它们弹出时按 OK.在某些情况下,如果出现特定的弹出窗口,我还需要重新开始我的循环.我假设这就是程序员所说的事件.
Sometimes when running SAP I have extra windows appear. I just need to press OK when they pop up. In some cases, I also need to start my loop all over again if a specific pop up window appears. I assume that this is what programmers call events.
在我的代码中,我有两种情况:
In my code I have two situations:
- 当一个窗口出现并且用户只需要按 OK 并按照正在进行的代码操作时.为此,我写了以下内容:
If Session.findById("wnd[2]/tbar[0]/btn[0]") Then
Session.findById("wnd[2]/tbar[0]/btn[0]").press
End If
- 当一个窗口出现并且循环必须在下一个 i 恢复时.我不知道如何处理这种情况.该窗口的代码行如下:
Session.findById("wnd[1]/tbar[0]/btn[0]")
我的问题是我无法在 SAP 中设置新会话,因为我有具有相同标题/行的弹出窗口.例如:wnd[2]/tbar[0]/btn[0]"或wnd[1]/tbar[0]/btn[0]"在某些情况下,我希望脚本启动一个新会话并在在某些情况下,我只是希望它在按 OK 后继续前进.
My problem is that I cannot set a new session in SAP because I have pop up windows that have the same titles/lines.For example: "wnd[2]/tbar[0]/btn[0]" or "wnd[1]/tbar[0]/btn[0]" In some cases I want the script to launch a new session and in some cases I just want it to proceed forward after pressing OK.
非常感谢您的反馈和指导.谢谢.
I highly appreciate your feedback and guidance. Thank you.
Sub Data_Satan()
...
Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "/nCT04"
Session.findById("wnd[0]/tbar[0]/btn[0]").press
Set objExcel = GetObject(, "Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
COL3 = Trim(CStr(objSheet.Range("C2").Value)) 'Column3
COL4 = Trim(CStr(objSheet.Range("D2").Value)) 'Column4
Session.findById("wnd[0]/usr/subCHARACT:SAPLCTMV:2000/subHEADER:SAPLCTMV:1100/ctxtRCTAV-ATNAM").Text = COL3
Session.findById("wnd[0]/usr/subCHARACT:SAPLCTMV:2000/subHEADER:SAPLCTMV:1100/ctxtRCTAV-ATNAM").caretPosition = 13
Session.findById("wnd[0]/usr/subCHARACT:SAPLCTMV:2000/subHEADER:SAPLCTMV:1100/btnDISPLAY").press
Session.findById("wnd[0]/mbar/menu[4]/menu[0]").Select
Session.findById("wnd[0]/usr/chkGF_DEP").Selected = True
Session.findById("wnd[0]/usr/ctxtCAWN-ATWRT").Text = COL4
Session.findById("wnd[0]/usr/chkGF_DEP").SetFocus
Session.findById("wnd[0]/tbar[1]/btn[8]").press
Dim myTree As Object
Dim RowCount, rows, i, j As Integer
Set myTree = Session.findById("wnd[0]/usr/cntlUSAGE_TREE_CONTAINER/shellcont/shell/shellcont[1]/shell[1]")
RowCount = myTree.GetColumnCol(myTree.GetColumnNames.Item(0)).Length
rows = RowCount - 1
For i = 5 To rows
j = i - 3
myTree.selectedNode Right(" " + CStr(i), 11)
myTree.doubleClickNode Right(" " + CStr(i), 11)
Session.findById("wnd[0]/mbar/menu[4]/menu[0]").Select
On Error Resume Next
'If Session.findById("wnd[1]/tbar[0]/btn[0]") Then
'Session.findById("wnd[1]/tbar[0]/btn[0]").press
'Session.findById("wnd[0]/tbar[0]/btn[3]").press
'End If
'WE REACHED THE TABLE
Session.findById("wnd[0]/usr/lbl[6,8]").SetFocus
Session.findById("wnd[0]/usr/lbl[6,8]").caretPosition = 1
Session.findById("wnd[0]").sendVKey 2
Session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/ctxtRC29P-IDNRK").SetFocus
Session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/ctxtRC29P-IDNRK").caretPosition = 5
Session.findById("wnd[0]").sendVKey 2
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27").Select
Session.findById("wnd[1]/usr/ctxtRMMG1-WERKS").Text = "0600"
Session.findById("wnd[1]/usr/ctxtRMMG1-WERKS").caretPosition = 4
Session.findById("wnd[1]/tbar[0]/btn[0]").press
If Session.findById("wnd[2]/tbar[0]/btn[0]") Then
Session.findById("wnd[2]/tbar[0]/btn[0]").press
End If
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2953/txtMBEW-STPRS").SetFocus
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2953/txtMBEW-STPRS").caretPosition = 0
cost = Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2953/txtMBEW-STPRS").Text
Range("G" & j) = cost 'Returns the most recent cost of a production part
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/ctxtRMMG1-MATNR").SetFocus
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/ctxtRMMG1-MATNR").caretPosition = 7
material = Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/ctxtRMMG1-MATNR").Text
Range("E" & j) = material 'Returns a production part number
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/txtMAKT-MAKTX").SetFocus
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/txtMAKT-MAKTX").caretPosition = 8
description = Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/txtMAKT-MAKTX").Text
Range("F" & j) = description 'Returns production part description
Next i
End Sub
推荐答案
我想我找到了解决方案.这是我的代码:
I think I found the solution. Here is my code:
Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "/nCT04"
Session.findById("wnd[0]/tbar[0]/btn[0]").press
Set objExcel = GetObject(, "Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
COL3 = Trim(CStr(objSheet.Range("C2").Value)) 'Column3
COL4 = Trim(CStr(objSheet.Range("D2").Value)) 'Column4
Session.findById("wnd[0]/usr/subCHARACT:SAPLCTMV:2000/subHEADER:SAPLCTMV:1100/ctxtRCTAV-ATNAM").Text = COL3
Session.findById("wnd[0]/usr/subCHARACT:SAPLCTMV:2000/subHEADER:SAPLCTMV:1100/ctxtRCTAV-ATNAM").caretPosition = 13
Session.findById("wnd[0]/usr/subCHARACT:SAPLCTMV:2000/subHEADER:SAPLCTMV:1100/btnDISPLAY").press
Session.findById("wnd[0]/mbar/menu[4]/menu[0]").Select
Session.findById("wnd[0]/usr/chkGF_DEP").Selected = True
Session.findById("wnd[0]/usr/ctxtCAWN-ATWRT").Text = COL4
Session.findById("wnd[0]/usr/chkGF_DEP").SetFocus
Session.findById("wnd[0]/tbar[1]/btn[8]").press
Dim myTree As Object
Dim RowCount, rows, i, j As Integer
Set myTree = Session.findById("wnd[0]/usr/cntlUSAGE_TREE_CONTAINER/shellcont/shell/shellcont[1]/shell[1]")
RowCount = myTree.GetColumnCol(myTree.GetColumnNames.Item(0)).Length
rows = RowCount - 1
For i = 5 To rows
j = i - 3
myTree.selectedNode Right(" " + CStr(i), 11)
myTree.doubleClickNode Right(" " + CStr(i), 11)
Session.findById("wnd[0]/mbar/menu[4]/menu[0]").Select
If Session.ActiveWindow.Name = "wnd[1]" Then
'Session.findById("wnd[1]/tbar[0]/btn[0]").press
Session.findById("wnd[1]").sendVKey 0
Session.findById("wnd[0]/tbar[0]/btn[3]").press
GoTo NextIteration
End If
'WE REACHED THE TABLE
Session.findById("wnd[0]/usr/lbl[6,8]").SetFocus
Session.findById("wnd[0]/usr/lbl[6,8]").caretPosition = 1
Session.findById("wnd[0]").sendVKey 2
Session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/ctxtRC29P-IDNRK").SetFocus
Session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/ctxtRC29P-IDNRK").caretPosition = 5
Session.findById("wnd[0]").sendVKey 2
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27").Select
Session.findById("wnd[1]/usr/ctxtRMMG1-WERKS").Text = "0600"
Session.findById("wnd[1]/usr/ctxtRMMG1-WERKS").caretPosition = 4
Session.findById("wnd[1]/tbar[0]/btn[0]").press
If Session.ActiveWindow.Name = "wnd[2]" Then
'Session.findById("wnd[2]/tbar[0]/btn[0]").press
Session.findById("wnd[2]").sendVKey 0
End If
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2953/txtMBEW-STPRS").SetFocus
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2953/txtMBEW-STPRS").caretPosition = 0
cost = Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2953/txtMBEW-STPRS").Text
Range("G" & j) = cost 'Returns the most recent cost of a production part
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/ctxtRMMG1-MATNR").SetFocus
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/ctxtRMMG1-MATNR").caretPosition = 7
material = Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/ctxtRMMG1-MATNR").Text
Range("E" & j) = material 'Returns a production part number
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/txtMAKT-MAKTX").SetFocus
Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/txtMAKT-MAKTX").caretPosition = 8
description = Session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/txtMAKT-MAKTX").Text
Range("F" & j) = description 'Returns production part description
Session.findById("wnd[0]/tbar[0]/btn[3]").press
Session.findById("wnd[0]/tbar[0]/btn[3]").press
Session.findById("wnd[0]/tbar[0]/btn[3]").press
Session.findById("wnd[0]/tbar[0]/btn[3]").press
NextIteration:
Next i
End Sub
这篇关于出现窗口时,我的代码无法设置新会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!