REPORT ZPCTLIST.

************************************************************
*  This little program uses the %_list internal table to
*  do various things with a list that is created by an
*  ABAP: save it on UNIX, save it on the pc, print it out
*  and send it out as an express mail. This method can save
*  a lots of hassle, because the form routines has to be
*  called only once, at the end of the report.
***********************************************************

INCLUDE <%_LIST>.

DATA: PARAMS LIKE PRI_PARAMS, VALID.
DATA: BEGIN OF DATA_TAB OCCURS 20,
        LINE(255),
END OF DATA_TAB.
DATA: BEGIN OF EMAIL_DATA.
        INCLUDE STRUCTURE SODOCCHGI1.
DATA: END OF EMAIL_DATA.
DATA: BEGIN OF EMAIL_SEND OCCURS 10.
        INCLUDE STRUCTURE SOMLRECI1.
DATA: END OF EMAIL_SEND.


* Write to screen
WRITE: / 'First line on the screen'.
* Hundreds of lines of code ...
WRITE: / 'Second line on the screen'.
* Hundreds of lines of code ...
WRITE: / 'Third line on the screen'.

* AND AT THE AND OF THE PROGRAM:
PERFORM SAVE_ON_UNIX.
PERFORM SAVE_ON_PC.
PERFORM PRINT_IT_OUT.
PERFORM SEND_EXPERESS_MAIL.

*---------------------------------------------------------------------*
*       FORM SAVE_ON_UNIX                                             *
*---------------------------------------------------------------------*
FORM SAVE_ON_UNIX.
  OPEN DATASET '/usr/sap/trans/listtest' FOR OUTPUT IN TEXT MODE.
  LOOP AT %_LIST.
    TRANSFER %_LIST-LINE TO '/usr/sap/trans/listtest'.
  ENDLOOP.
  CLOSE DATASET '/usr/sap/trans/listtest'.
ENDFORM.

*---------------------------------------------------------------------*
*       FORM SAVE_ON_PC                                               *
*---------------------------------------------------------------------*
FORM SAVE_ON_PC.
  LOOP AT %_LIST.
    DATA_TAB-LINE = %_LIST-LINE.
    APPEND DATA_TAB.
  ENDLOOP.
  CALL FUNCTION 'WS_DOWNLOAD'
       EXPORTING
            FILENAME = 'c:\listtest'
       TABLES
            DATA_TAB = DATA_TAB.
ENDFORM.

*---------------------------------------------------------------------*
*       FORM PRINT_IT_OUT                                             *
*---------------------------------------------------------------------*
FORM PRINT_IT_OUT.

  CALL FUNCTION 'GET_PRINT_PARAMETERS'
       EXPORTING
            DESTINATION    = 'AA01'
            LIST_NAME      = 'List'
            LIST_TEXT      = 'List from %_list'
            IMMEDIATELY    = ' '
            RELEASE        = 'X'
            NEW_LIST_ID    = 'X'
            NO_DIALOG      = 'X'
       IMPORTING
            OUT_PARAMETERS = PARAMS
            VALID          = VALID.
  IF VALID <> SPACE.
    NEW-PAGE PRINT ON PARAMETERS PARAMS NO DIALOG.
    LOOP AT %_LIST.
      WRITE: / %_LIST-LINE.
    ENDLOOP.
  ENDIF.
  NEW-PAGE PRINT OFF.
ENDFORM.

*---------------------------------------------------------------------*
*       FORM SEND_EXPERESS_MAIL                                       *
*---------------------------------------------------------------------*
FORM SEND_EXPERESS_MAIL.
  EMAIL_DATA-OBJ_NAME = 'MESSAGE'.
  EMAIL_DATA-OBJ_DESCR = 'TEST MAIL THAT USES %_LIST'.
  EMAIL_DATA-OBJ_LANGU = 'E'.
  EMAIL_DATA-SENSITIVTY = 'P'.
  EMAIL_DATA-OBJ_PRIO =  '1'.
  EMAIL_DATA-NO_CHANGE = 'X'.
  EMAIL_DATA-PRIORITY = '1'.

  EMAIL_SEND-RECEIVER = 'KABAIIM'.
  EMAIL_SEND-REC_TYPE = 'B'.
  EMAIL_SEND-EXPRESS = 'X'.
  APPEND EMAIL_SEND.

  CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
       EXPORTING
            DOCUMENT_DATA  = EMAIL_DATA
            DOCUMENT_TYPE  = 'RAW'
            PUT_IN_OUTBOX  = 'X'
       TABLES
            OBJECT_CONTENT = DATA_TAB
            RECEIVERS      = EMAIL_SEND.
ENDFORM.