Search code examples
excelvbasap-guialv

Save SM37 rows with sorting via GUI Script?


The following SAP GUI Script for SAP 740 saves the SM37 list to a text file.

Before saving the list, I'd like to sort the list by Start date and Start time.

I can display the popup to select Sort fields. On left side there is a table control with current Sort columns (job name, created by, progress, scheduled start date, scheduled start time), on right side there is a table control with all possible Sort columns, each of these left and right Sort columns can be selected, and between the two table controls there are buttons to move Sort columns from left to right or vice versa.

Now, to select Sort columns, I have to select rows of table controls, but I don't know how to do it. Can you help me?

Public Sub RunSM37Cancelled()

Dim W_Ret As Boolean

W_Ret = Attach_Session
If Not W_Ret Then
    Exit Sub
End If

On Error GoTo disperr

objSess.findById("wnd[0]").maximize
objSess.findById("wnd[0]/tbar[0]/okcd").Text = "sm37"
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/txtBTCH2170-JOBNAME").Text = "*"
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/txtBTCH2170-USERNAME").Text = "*"
objSess.findById("wnd[0]/usr/txtBTCH2170-USERNAME").SetFocus
objSess.findById("wnd[0]/usr/txtBTCH2170-USERNAME").caretPosition = 1
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/chkBTCH2170-SCHEDUL").Selected = False
objSess.findById("wnd[0]/usr/chkBTCH2170-READY").Selected = False
objSess.findById("wnd[0]/usr/chkBTCH2170-RUNNING").Selected = False
objSess.findById("wnd[0]/usr/chkBTCH2170-FINISHED").Selected = False
objSess.findById("wnd[0]/usr/chkBTCH2170-ABORTED").Selected = True
objSess.findById("wnd[0]/usr/ctxtBTCH2170-FROM_DATE").Text = Format(Now()-1,"mm/dd/yyyy")
objSess.findById("wnd[0]/usr/ctxtBTCH2170-FROM_DATE").SetFocus
objSess.findById("wnd[0]/usr/ctxtBTCH2170-FROM_DATE").caretPosition = 10
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/ctxtBTCH2170-TO_DATE").Text = Format(Now(), "mm/dd/yyyy")
objSess.findById("wnd[0]/usr/ctxtBTCH2170-TO_DATE").SetFocus
objSess.findById("wnd[0]/usr/ctxtBTCH2170-TO_DATE").caretPosition = 10
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/tbar[1]/btn[8]").press
objSess.findById("wnd[0]/usr/lbl[80,10]").SetFocus
objSess.findById("wnd[0]/usr/lbl[80,10]").caretPosition = 5
objSess.findById("wnd[0]").sendVKey 2
objSess.findById("wnd[0]/usr/lbl[91,10]").SetFocus
objSess.findById("wnd[0]/usr/lbl[91,10]").caretPosition = 4
objSess.findById("wnd[0]").sendVKey 2

' Display sort dialog (41 = Ctrl+Shift+F5 = menu Edit > Sort in ascending order)
objSess.findById("wnd[0]/tbar[1]/btn[41]").press

' <<<< Here, how to select lines in left and right table controls?

' Downloading the file
fileobjSess.findById("wnd[0]/mbar/menu[5]/menu[5]/menu[2]/menu[1]").Select
objSess.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select
objSess.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").SetFocus
objSess.findById("wnd[1]/tbar[0]/btn[0]").press
objSess.findById("wnd[1]/usr/ctxtDY_PATH").Text = ffilepath
objSess.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = ffilename
objSess.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 8
objSess.findById("wnd[1]/tbar[0]/btn[11]").press

Exit Sub

disperr:
   MsgBox "Error occured while retrieving data", vbCritical + vbOKOnly

End Sub

Solution

  • To select a row by its position in a table control, you need to determine the name of the table control, then use the method tableControl.getAbsoluteRow(row) to get the instance of the row, and set its property Selected = True. Example :

    tableControl = objSess.findById("wnd[1]/usr/tblSAPLSKBHTC_FIELD_LIST_820")
    tableControl.getAbsoluteRow(6).Selected = True
    

    Now, to answer your specific question (ALV sorting in SM37), you also need to press additional buttons to select the Sort fields.

    The Sort dialog screen is made of two table controls. You may first press the button "Hide all fields" to move all fields to the right table control (fields not selected), then select the fields on the right table control (as explained in the first paragraph) and press the buttons "Show sel. fields (Ctrl+F3)" and "Copy (Enter)".

    You need to know the row number of the field you want to select in the right table control. In an ABAP 7.52 system, these fields are listed in this sequence (first one is row 1, second one is row 2 and so on):

    1. Job name
    2. Spool list
    3. Job documentation
    4. created by
    5. status
    6. start date
    7. start time
    8. duration
    9. ...

    You also need to know the name of the right table control. For that, you may select the menu System > Status, double click the number of "screen/dynpro" which displays the technical names of screen elements. In your case, you see TC_FIELD_LIST_820. You also see that the screen belongs to the program SAPLSKBH.

    The final program would look like this:

    ffilepath = "your path here"
    ffilename = "Test.txt"
    
    
    With objSess
        .findById("wnd[0]").Maximize
        .findById("wnd[0]/tbar[0]/okcd").Text = "/nsm37"
        .findById("wnd[0]").sendVKey 0
        .findById("wnd[0]/usr/txtBTCH2170-JOBNAME").Text = "*"
        .findById("wnd[0]/usr/txtBTCH2170-USERNAME").Text = "*"
        .findById("wnd[0]/usr/chkBTCH2170-SCHEDUL").Selected = False
        .findById("wnd[0]/usr/chkBTCH2170-READY").Selected = False
        .findById("wnd[0]/usr/chkBTCH2170-RUNNING").Selected = False
        .findById("wnd[0]/usr/chkBTCH2170-FINISHED").Selected = False
        .findById("wnd[0]/usr/chkBTCH2170-ABORTED").Selected = True
        .findById("wnd[0]/usr/ctxtBTCH2170-FROM_DATE").Text = Format(Now() - 1, "mm/dd/yyyy")
        ' .findById("wnd[0]/usr/ctxtBTCH2170-FROM_DATE").Text = Format(Now() - 1, "dd/mm/yyyy")
        .findById("wnd[0]/usr/ctxtBTCH2170-TO_DATE").Text = Format(Now(), "mm/dd/yyyy")
        ' .findById("wnd[0]/usr/ctxtBTCH2170-TO_DATE").Text = Format(Now(), "dd/mm/yyyy")
    
        .findById("wnd[0]/tbar[1]/btn[8]").press
    
        ' Display sort dialog (41 = Ctrl+Shift+F5 = menu Edit > Sort in ascending order)
        .findById("wnd[0]/tbar[1]/btn[41]").press
    
        ' Remove all previous sort fields (press pushbutton named "APP_FL_ALL")
        .findById("wnd[1]/usr/btnAPP_FL_ALL").press
    
        ' Select sort field(s) (row 6=start date, row 7=start time)
        with .findById("wnd[1]/usr/tblSAPLSKBHTC_FIELD_LIST_820")
            .getAbsoluteRow(6).Selected = True
            .getAbsoluteRow(7).Selected = True
        end with
    
        ' Press pushbutton "Show sel. fields"
        .findById("wnd[1]/usr/btnAPP_WL_SING").press
    
        ' Press key "Enter" (corresponds to pushbutton "Copy (Enter)")
        .findById("wnd[1]/tbar[0]/btn[0]").press
    
        ' Downloading the file
        .findById("wnd[0]/mbar/menu[5]/menu[5]/menu[2]/menu[1]").Select
        .findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select
        .findById("wnd[1]/tbar[0]/btn[0]").press
        .findById("wnd[1]/usr/ctxtDY_PATH").Text = ffilepath
        .findById("wnd[1]/usr/ctxtDY_FILENAME").Text = ffilename
        .findById("wnd[1]/tbar[0]/btn[11]").press
    End With