Lucas Tezolini
Lucas Tezolini

Reputation: 35

select until the last item in SAP GUI

I'm trying to automate this task in SAP GUI which selects two columns and then page down until the last item, and copy the selection.

I recorded a script in SAP GUI (see at the end). This line of the script is responsible to page down and it stops when the bottom is found:

session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell" _
).firstVisibleRow = NUMBER

But this NUMBER always changes. I can't say the last visible row is 577 or 600 or 900... I need to know the exact number every single time.

I think the While should be useful but I don't know how to verify it in SAP GUI. Any Ideas?

Recorded script:

Sub AUTOMATE()
    
    If Not IsObject(App) Then
       Set SapGuiAuto = GetObject("SAPGUI")
       Set App = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(Connection) Then
       Set Connection = App.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]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").setCurrentCell -1, "FEVOR"
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").firstVisibleRow = 84
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectColumn "FEVOR" 'the first column I want
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectColumn "AUFNR" 'the second column I want
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").firstVisibleRow = 140
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").firstVisibleRow = 280
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").firstVisibleRow = 336
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").firstVisibleRow = 476
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").firstVisibleRow = 532
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").firstVisibleRow = 577
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").contextMenu
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectContextMenuItemByPosition "0" 'copy the selection
    
    Range("F2").Select 'select the cell in my target sheet
    ActiveSheet.Paste 'paste it
    
End Sub

Upvotes: 1

Views: 2313

Answers (1)

cozmic
cozmic

Reputation: 11

Not sure if this is what you are looking for but to find the number of rows I use:

countRows = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").RowCount

then to find a specific item in a while loop:

po = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").getCellValue(j, "VBELN")

where po is the variable i want and j is the row number. I am using python, so the syntax might be different.

Upvotes: 1

Related Questions