-2

I have a situation when a table in SAP GUI does not have fields and I have to go back to the main table and start on a next iteration. I do not know how to declare this event in VBA. In other cases, I just double-click on lines. This table calls a debug window and script stops.

Can anyone give a hint on how to stop the debugger mode and go to the next iteration?

My problem is I do not know how to declare such an event within VBA.

Table with zero lines:

enter image description here

VBA code:

Sub Looping_Tree()

    Dim Application, Connection, Session As Object
    Set SapGuiAuto = GetObject("SAPGUI")
    Set Application = SapGuiAuto.GetScriptingEngine
    Set Connection = Application.Children(0)
    Set Session = Connection.Children(0)

    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]").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
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
OAltyn
  • 21
  • 8
  • I identified that the first column with ADAC_ value is under `session.findById("wnd[0]/usr/lbl[1,7]").caretPosition = 16` but I am still trying to figure out how to approach such a situation. – OAltyn Sep 04 '18 at 23:31
  • When the table has lines with values they start with `Session.findById("wnd[0]/usr/lbl[6,8]")` – OAltyn Sep 04 '18 at 23:33
  • The pop up window says "Control cannot be find by ID" – OAltyn Sep 04 '18 at 23:39
  • 3
    Posting the problem code increases your chances of getting an answer... – Tim Williams Sep 05 '18 at 04:50
  • @TimWilliams I added the code to the post (I specified in CAPS where my code get to the table content. – OAltyn Sep 05 '18 at 16:03
  • I tried to embed If Then Else statement: `If Session.findById("wnd[0]/usr/lbl[6,8]") Then Session.findById("wnd[0]/usr/lbl[6,8]").SetFocus Session.findById("wnd[0]/usr/lbl[6,8]").caretPosition = 1 Else Session.findById("wnd[0]/tbar[0]/btn[3]").press Session.findById("wnd[0]/tbar[0]/btn[3]").press GoTo NextIteration End If` But it says object does not support this method. What's wrong with it? – OAltyn Sep 05 '18 at 18:11
  • Please look at this link: https://archive.sap.com/discussions/thread/3502273 So you could either use the variable childcount or totscrol for the query. Unfortunately, I can not test it now because I'm already at home. – ScriptMan Sep 05 '18 at 18:28
  • @ScriptMan thanks for the hint! I will try to figure out how to implement it. I am getting into it but very slowly... – OAltyn Sep 05 '18 at 21:44
  • @ScriptMan I did a little progress in terms of understanding how to set i. I need lbl[6,8] to have in the table to double click on a line. If I don't have that line I am just going back to the tree. What childcount is for? – OAltyn Sep 06 '18 at 05:42
  • Related [My code cannot set a new session when a window appears](https://stackoverflow.com/q/52172874/692942) – user692942 Sep 06 '18 at 15:55

1 Answers1

0

From the link I found out the following variables for you:

...
'WE REACHED THE TABLE

set sapusr = session.findById("wnd[0]/usr")
sapusr.VerticalScrollbar.Position = 0
childcount = sapusr.Children.Count
lastchildID = sapusr.Children(childcount - 1).ID
visrow = CLng(Right(Left(lastchildID, Len(lastchildID) - 1), _
        Len(lastchildID) - InStr(1, lastchildID, ",") - 1))
totscrol = sapusr.VerticalScrollbar.Maximum
'msgbox lastchildID
'msgbox ChildCount
'msgbox totscrol
'msgbox visrow
If visrow > 7 Then 
   Session.findById("wnd[0]/usr/lbl[6,8]").SetFocus 
   Session.findById("wnd[0]/usr/lbl[6,8]").caretPosition = 1
Else 
   Session.findById("wnd[0]/tbar[0]/btn[3]").press     
   Session.findById("wnd[0]/tbar[0]/btn[3]").press 
   GoTo NextIteration 
End If
...

Regards, ScriptMan

ScriptMan
  • 1,580
  • 1
  • 9
  • 9
  • Hi @ScriptMan thank you so much for the guidance. I am definitely going to learn the theory to understand it at the deeper level. At this moment, as a beginner, I am just snowed under trials and errors... – OAltyn Sep 06 '18 at 21:32