0

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.

My questions:

  1. If error resume next: did I put it in the right place?

  2. Sometimes when running SAP GUI, 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:

  1. when a window appears and a user is just required to press OK and follow the code that is proceeding. for that, I wrote the following:
    If Session.findById("wnd[2]/tbar[0]/btn[0]") Then
      Session.findById("wnd[2]/tbar[0]/btn[0]").press
    End If
    
  2. when a window appears and the loop has to be resumed on the next i line. I am not sure how to handle this situation. The code line for such window is the following: Session.findById("wnd[1]/tbar[0]/btn[0]")

My problem is that I cannot set a new session in SAP GUI 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
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
OAltyn
  • 21
  • 8
  • Your "On Error Resume Next" are you trying to escape an error and resume the next iteration? If so, this VBA code will not do that. – cmprogram Sep 04 '18 at 19:34
  • 5
    If this code works as intended, I'd suggest posting on [Code Review](https://codereview.stackexchange.com/) instead. – Comintern Sep 04 '18 at 19:37
  • @Comintern It works but I also have some inquires on how to amend it. I am guessing there will be lots of bugs if I do not prevent some events appearing when the code is executing. – OAltyn Sep 04 '18 at 19:48
  • @cmprogram yes, I want the loop to start over if the following lines return error: myTree.selectedNode Right(" " + CStr(i), 11) myTree.doubleClickNode Right(" " + CStr(i), 11) Session.findById("wnd[0]/mbar/menu[4]/menu[0]").Select – OAltyn Sep 04 '18 at 19:51
  • 1
    On the other hand if you need help making it work as intended, then [edit] this post to remove the parts involving critique requests, and focus on one specific issue. – Mathieu Guindon Sep 04 '18 at 19:51
  • @MathieuGuindon thank you, I have removed some lines. – OAltyn Sep 04 '18 at 19:55

1 Answers1

0

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
OAltyn
  • 21
  • 8
  • @ScriptMan thanks a lot, I am really getting more into it. SAP and VBS are two things that will make my days at school. – OAltyn Sep 05 '18 at 16:05