Monday, 16 February 2015

    Miscellaneous Programs


Download internal table data into different (3) excel sheets based on fields
          
REPORT  ZH_EXCELSHEET.

INCLUDE ole2incl.
DATA:  application TYPE ole2_object,
              workbook    TYPE ole2_object,
             sheet       TYPE ole2_object,
            cells       TYPE ole2_object,

       BEGIN OF wa_lfa1,
       name1 type name1,
       land1 type land1,
       ort01 type ort01,
       END   OF wa_lfa1,

       it_lfa1  like TABLE OF wa_lfa1,
       v_row    type sy-tabix.

START-OF-SELECTION.
  SELECT name1 land1 ort01 fROM lfa1 INTO CORRESPONDING FIELDS OF TABLE it_lfa1 UP TO 15 ROWS.

  CREATE OBJECT application 'excel.application'.
  SET PROPERTY OF application 'visible' = 1.
  CALL METHOD OF application 'Workbooks' = workbook.
  CALL METHOD OF workbook 'Add'.
*******************************************1
* Create first Excel Sheet
  CALL METHOD OF application 'Worksheets' = sheet
                               EXPORTING #1 = 1.
  CALL METHOD OF sheet 'Activate'.
  SET PROPERTY OF sheet 'Name' = 'Sheet1'.
  clear v_row.
  LOOP AT it_lfa1 into wa_lfa1.
     v_row = sy-tabix.
      perform fill_cell  using  v_row 1  wa_lfa1-name1.
  ENDLOOP.
********************************************2
* Create second Excel sheet
  CALL METHOD OF application 'Worksheets' = sheet
                               EXPORTING #1 = 2.
  SET PROPERTY OF sheet 'Name' = 'Sheet2'.
  CALL METHOD OF sheet 'Activate'.
  clear v_row.
  LOOP AT it_lfa1 into wa_lfa1.
     v_row = sy-tabix.
      perform fill_cell using  v_row 1 wa_lfa1-land1.
  ENDLOOP.
******************************************3
* Create third Excel sheet
  CALL METHOD OF application 'Worksheets' = sheet
                               EXPORTING #1 = 3.
  SET PROPERTY OF sheet 'Name' = 'Sheet3'.
  CALL METHOD OF sheet 'Activate'.
  clear v_row.
  LOOP AT it_lfa1 into wa_lfa1.
     v_row = sy-tabix.
      perform fill_cell using  v_row 1 wa_lfa1-ort01.
  ENDLOOP.
*********************************************
form fill_cell  using row  col val.
    CALL METHOD OF sheet 'Cells' = cells EXPORTING #1 = row  #2 = col.
    SET PROPERTY OF cells 'Value' = val.
endform.                    " fill_cell
         
*********************************************
***INCLUDE OLE2INCL.
TYPE-POOLS OLE2.


TYPE-POOL OLE2.

TYPES:
  OLE2_OBJECT LIKE   OBJ_RECORD.
*    Object handle initialization
CONSTANTS:
  OLE2_OBJECT_HEADER TYPE OLE2_OBJECT-HEADER VALUE 'OBJH',
  OLE2_OBJECT_TYPE   TYPE OLE2_OBJECT-TYPE   VALUE 'OLE2',
  OLE2_OBJECT_HANDLE TYPE OLE2_OBJECT-HANDLE VALUE -1,
  BEGIN OF OLE2_OBJECT_INITIAL,
             HEADER   TYPE OLE2_OBJECT-HEADER    VALUE OLE2_OBJECT_HEADER,
            TYPE     TYPE OLE2_OBJECT-TYPE      VALUE OLE2_OBJECT_TYPE,
            HANDLE   TYPE OLE2_OBJECT-HANDLE    VALUE OLE2_OBJECT_HANDLE,
            CB_INDEX TYPE OLE2_OBJECT-CB_INDEX  VALUE SPACE,
            CLSID    TYPE OLE2_OBJECT-CLSID     VALUE SPACE,
  END OF OLE2_OBJECT_INITIAL.

CONSTANTS: OLE2_%_POINTER POINTER.
TYPES: BEGIN OF OLE2_PCB,
              PCBID TYPE I,
              DATACB LIKE OLE2_%_POINTER,
       END OF OLE2_PCB.

TYPES BEGIN OF OLE2_METH_PARMS.
         INCLUDE STRUCTURE SWCONT.
         TYPES POINTER TYPE OLE2_PCB.
TYPES END OF   OLE2_METH_PARMS.

TYPES:
  OLE2_METH_PARMS_TAB TYPE OLE2_METH_PARMS OCCURS 0,
*    Method Parameter Table: contains the methoid parameter
*      types and values exporting and importing parameters.
  OLE2_LCID TYPE I,
*    Locale Id: determines the language and other settings
*      (like value formats) of the automation server.
*      For more information see: Include OLE2LCID
  OLE2_TYPE TYPE I.
*    OLE Variant Type: determines the "variant type" for the
*      parameters of Automation Controller requests.
*      For more information see: Include OLE2TYPE

TYPES: OLE2_PARAMETER LIKE SWCBCONT-VALUE.
         

       output:






This program demonstrates hoe to read data from multiple worksheets of an excel file.
I developed this code using SAP class:
CL_GUI_CUSTOM_CONTAINER
and intefaces:
I_OI_CONTAINER_CONTROL,
I_OI_DOCUMENT_PROXY AND
I_OI_SPREADSHEET.


REPORT  ztest_sourav_excel NO STANDARD PAGE HEADING.

DATA:
oref_container   TYPE REF TO cl_gui_custom_container,
iref_control     TYPE REF TO i_oi_container_control,
iref_document    TYPE REF TO i_oi_document_proxy,
iref_spreadsheet TYPE REF TO i_oi_spreadsheet,
iref_error       TYPE REF TO i_oi_error.

DATA:
v_document_url TYPE LENGTH 256,
i_sheets TYPE soi_sheets_table,
wa_sheets TYPE soi_sheets,
i_data        TYPE soi_generic_table,
wa_data       TYPE soi_generic_item,
i_ranges      TYPE soi_range_list.

PARAMETERS:
p_file  TYPE  localfile OBLIGATORY,
p_rows TYPE DEFAULT 100 OBLIGATORY,   "Rows (Maximum 65536)
p_cols TYPE DEFAULT 10 OBLIGATORY.    "Columns (Maximum 256)

INITIALIZATION.

  CALL METHOD c_oi_container_control_creator=>get_container_control
    IMPORTING
      control iref_control
      error   iref_error.
  IF iref_error->has_failed 'X'.
    CALL METHOD iref_error->raise_message
      EXPORTING
        type 'E'.
  ENDIF.


  CREATE OBJECT oref_container
    EXPORTING
      container_name              'CONT'
    EXCEPTIONS
      cntl_error                  1
      cntl_system_error           2
      create_error                3
      lifetime_error              4
      lifetime_dynpro_dynpro_link 5
      OTHERS                      6.
  IF sy-subrc <> 0.
    MESSAGE e001(00WITH 'Error while creating container'.
  ENDIF.

  CALL METHOD iref_control->init_control
    EXPORTING
      inplace_enabled      'X'
      r3_application_name  'EXCEL CONTAINER'
      parent               oref_container
    IMPORTING
      error                iref_error
    EXCEPTIONS
      javabeannotsupported 1
      OTHERS               2.
  IF iref_error->has_failed 'X'.
    CALL METHOD iref_error->raise_message
      EXPORTING
        type 'E'.
  ENDIF.

  CALL METHOD iref_control->get_document_proxy
    EXPORTING
      document_type  soi_doctype_excel_sheet
    IMPORTING
      document_proxy iref_document
      error          iref_error.
  IF iref_error->has_failed 'X'.
    CALL METHOD iref_error->raise_message
      EXPORTING
        type 'E'.
  ENDIF.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

* To provide F4 help for the file
  PERFORM sub_file_f4.

START-OF-SELECTION.

  CONCATENATE 'FILE://' p_file INTO v_document_url.

  CALL METHOD iref_document->open_document
    EXPORTING
      document_title 'Excel'
      document_url   v_document_url
      open_inplace   'X'
    IMPORTING
      error          iref_error.
  IF iref_error->has_failed 'X'.
    CALL METHOD iref_error->raise_message
      EXPORTING
        type 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.

  CALL METHOD iref_document->get_spreadsheet_interface
    EXPORTING
      no_flush        ' '
    IMPORTING
      error           iref_error
      sheet_interface iref_spreadsheet.

  IF iref_error->has_failed 'X'.
    CALL METHOD iref_error->raise_message
      EXPORTING
        type 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.

  CALL METHOD iref_spreadsheet->get_sheets
    EXPORTING
      no_flush ' '
    IMPORTING
      sheets   i_sheets
      error    iref_error.
  IF iref_error->has_failed 'X'.
    CALL METHOD iref_error->raise_message
      EXPORTING
        type 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.
  LOOP AT i_sheets INTO wa_sheets.
    CALL METHOD iref_spreadsheet->select_sheet
      EXPORTING
        name  wa_sheets-sheet_name
      IMPORTING
        error iref_error.
    IF iref_error->has_failed 'X'.
      EXIT.
    ENDIF.
    CALL METHOD iref_spreadsheet->set_selection
      EXPORTING
        top     1
        left    1
        rows    p_rows
        columns p_cols.

    CALL METHOD iref_spreadsheet->insert_range
      EXPORTING
        name     'Test'
        rows     p_rows
        columns  p_cols
        no_flush ''
      IMPORTING
        error    iref_error.
    IF iref_error->has_failed 'X'.
      EXIT.
    ENDIF.

    REFRESH i_data.

    CALL METHOD iref_spreadsheet->get_ranges_data
      EXPORTING
        all      'X'
      IMPORTING
        contents i_data
        error    iref_error
      CHANGING
        ranges   i_ranges.
    DELETE i_data WHERE value IS INITIAL OR value space.
    ULINE.
    WRITE:/1 wa_sheets-sheet_name COLOR 3.
    ULINE.

    LOOP AT i_data INTO wa_data.
      WRITE:(50wa_data-value.
      AT END OF row.
        NEW-LINE.
      ENDAT.
    ENDLOOP.
  ENDLOOP.

  CALL METHOD iref_document->close_document
    IMPORTING
      error iref_error.
  IF iref_error->has_failed 'X'.
    CALL METHOD iref_error->raise_message
      EXPORTING
        type 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.

  CALL METHOD iref_document->release_document
    IMPORTING
      error iref_error.
  IF iref_error->has_failed 'X'.
    CALL METHOD iref_error->raise_message
      EXPORTING
        type 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  SUB_FILE_F4
*&---------------------------------------------------------------------*
*       F4 help for file path
*----------------------------------------------------------------------*
FORM sub_file_f4 .
  DATA:
  l_desktop       TYPE string,
  l_i_files       TYPE filetable,
  l_wa_files      TYPE file_table,
  l_rcode         TYPE int4.

* Finding desktop
  CALL METHOD cl_gui_frontend_services=>get_desktop_directory
    CHANGING
      desktop_directory    l_desktop
    EXCEPTIONS
      cntl_error           1
      error_no_gui         2
      not_supported_by_gui 3
      OTHERS               4.
  IF sy-subrc <> 0.
    MESSAGE e001(00WITH
    'Desktop not found'.
  ENDIF.

* Update View
  CALL METHOD cl_gui_cfw=>update_view
    EXCEPTIONS
      cntl_system_error 1
      cntl_error        2
      OTHERS            3.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            'Select Excel file'
      default_extension       '.xls'
      file_filter             '.xls'
      initial_directory       l_desktop
    CHANGING
      file_table              l_i_files
      rc                      l_rcode
    EXCEPTIONS
      file_open_dialog_failed 1
      cntl_error              2
      error_no_gui            3
      not_supported_by_gui    4
      OTHERS                  5.
  IF sy-subrc <> 0.
    MESSAGE e001(00WITH 'Error while opening file'.
  ENDIF.

  READ TABLE l_i_files INDEX INTO l_wa_files.
  IF sy-subrc 0.
    p_file l_wa_files-filename.
  ELSE.
    MESSAGE e001(00WITH 'Error while opening file'.
  ENDIF.

ENDFORM.                    " SUB_FILE_F4.


input: 

output:



Program to upload attachments and send it to the required Email ID


*&---------------------------------------------------------------------*
*& REPORT  zattach_email                                                *
*&---------------------------------------------------------------------*
*& Description: Program to upload attachments and send it to the       *
*&              required Email ID                                      *
*&---------------------------------------------------------------------*
*& Modification Log:                                                   *
*&                                                                     *
*& Date       Name                User ID             Transport #      *
*&            Description of Modifications                             *
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
REPORT  zattach_email  MESSAGE-ID zz
                       LINE-SIZE 132
                       NO STANDARD PAGE HEADING.
*----------------------------------------------------------------------*
*                     TABLES                                           *
*----------------------------------------------------------------------*
TABLESsomlreci1.

*----------------------------------------------------------------------*
*                     GLOBAL DATA DECLARATION                          *
*----------------------------------------------------------------------*
DATAt_mailhex   TYPE STANDARD TABLE OF solix,
      t_contents  TYPE STANDARD TABLE OF solisti1,
      wa_contents TYPE solisti1,
      w_file      TYPE dsvasdocid,
      w_extn(5)   TYPE c,
      w_mail_subj TYPE string,
      w_document  TYPE REF TO cl_document_bcs.

*----------------------------------------------------------------------*
*                     CONSTANTS DECLARATION                            *
*----------------------------------------------------------------------*
CONSTANTS:
*-- Constants used in the body of the Email (HTML)
c_htm         TYPE char3   VALUE 'HTM',
c_style_start TYPE char255 VALUE '',
c_new_line    TYPE char255 VALUE '
'
,
c_space(6)    TYPE c       VALUE ' ',

*-- Used as an Example for displaying space between texts in Email body
c_emp1(6)     TYPE c       VALUE 101001,
c_emp2(6)     TYPE c       VALUE 101002,
c_emp3(6)     TYPE c       VALUE 101003.

*----------------------------------------------------------------------*
*                     SELECTION SCREEN                                 *
*----------------------------------------------------------------------*
*-- Input Details - Block
SELECTION-SCREEN BEGIN OF BLOCK file WITH FRAME TITLE text-t01.
PARAMETERSp_attach   TYPE  rlgrap-filename.
SELECTION-SCREEN END OF BLOCK file.
*-- Email ID of the Recipient
SELECTION-SCREEN BEGIN OF BLOCK mail WITH FRAME TITLE text-t02.
SELECT-OPTIONSs_mailid  FOR somlreci1-receiver NO INTERVALS.
SELECTION-SCREEN END OF BLOCK mail.

*----------------------------------------------------------------------*
*                          AT SELECTION SCREEN                         *
*----------------------------------------------------------------------*
*-- Providing F4 Help for the input file
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_attach.
  PERFORM file_path USING 'P_ATTACH'.

*&---------------------------------------------------------------------*
*                       START-OF-SELECTION                             *
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  IF p_attach IS INITIAL.
    MESSAGE i999(zzWITH 'Please Enter at-least one file name'(001).
    EXIT.
  ENDIF.
*-- Upload data from Presentation Server to SAP
  PERFORM upload_data.

*----------------------------------------------------------------------*
*                        END OF SELECTION                              *
*----------------------------------------------------------------------*
END-OF-SELECTION.
*-- Frame the Body of the Email
  PERFORM frame_mail_body.
*-- Send Mail
  PERFORM send_mail.

*&--------------------------------------------------------------------*
*& Form Name      : File_path
*&--------------------------------------------------------------------*
*& Description    : F4 help for File Name / Path
*&--------------------------------------------------------------------*
*& Parameters     : FP_FILE  --> File Name
*&--------------------------------------------------------------------*
FORM file_path USING fp_file TYPE any.
*-- Selects the directory list
  CALL FUNCTION 'F4_DXFILENAME_4_DYNP'
    EXPORTING
      dynpfield_filename fp_file
      dyname             sy-cprog
      dynumb             sy-dynnr
      filetype           'P'
      location           'P'
      server             space.

ENDFORM.                    " file_path
*&---------------------------------------------------------------------*
*& Form Name     : Upload_Data                                         *
*&---------------------------------------------------------------------*
*& Description   : Upload data into SAP from Presentation Server       *
*&---------------------------------------------------------------------*
*& Parameters    : None                                                *
*&---------------------------------------------------------------------*
FORM upload_data.
*-- Local data declaration
  DATAl_file  TYPE string,
        l_index TYPE sy-tabix,
*-- For holding the split file name
        tl_splitfile TYPE STANDARD TABLE OF rlgrap-filename,
        wl_splitfile TYPE rlgrap-filename.

  l_file p_attach.

**-- Function module to split the Filename and Extension from the Path
  CALL FUNCTION 'CH_SPLIT_FILENAME'
    EXPORTING
      complete_filename l_file
    IMPORTING
      extension         w_extn
      name              w_file.

*-- Split the filename at '.'
  SPLIT l_file AT '.' INTO TABLE tl_splitfile.
  DESCRIBE TABLE tl_splitfile LINES l_index.

*-- In case the filename contains more than one dot
  IF l_index GT 2.
    CLEARwl_splitfilew_extn.
*-- Get the Extension of the file
    READ TABLE tl_splitfile INTO wl_splitfile INDEX l_index.
    w_extn wl_splitfile.
    DELETE tl_splitfile INDEX l_index.
    DELETE tl_splitfile INDEX 1.
    CLEAR wl_splitfile.
*-- Get the Actual filename
    LOOP AT tl_splitfile INTO wl_splitfile.
      CONCATENATE '.' wl_splitfile INTO wl_splitfile.
    ENDLOOP.
    CONCATENATE w_file wl_splitfile INTO w_file.
  ENDIF.

  CONDENSE w_extn.

*-- Upload File
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                l_file
      filetype                'BIN'
    TABLES
      data_tab                t_mailhex
    EXCEPTIONS
      file_open_error         1
      file_read_error         2
      no_batch                3
      gui_refuse_filetransfer 4
      invalid_type            5
      no_authority            6
      unknown_error           7
      bad_data_format         8
      header_not_allowed      9
      separator_not_allowed   10
      header_too_long         11
      unknown_dp_error        12
      access_denied           13
      dp_out_of_memory        14
      disk_full               15
      dp_timeout              16
      OTHERS                  17.

  IF sy-subrc IS NOT INITIAL.
    MESSAGE i999(zzWITH 'Error in reading file for upload'(002)
    w_file.
  ENDIF.
ENDFORM.                    " upload_data

*&---------------------------------------------------------------------*
*& Form Name     : frame_mail_body                                     *
*&---------------------------------------------------------------------*
*& Description   : To frame the body part of the Email to be sent      *
*&---------------------------------------------------------------------*
*& Parameters    : None                                                *
*&---------------------------------------------------------------------*
FORM frame_mail_body.
*-- Local data declaration to hold the textpool
  DATAtl_textpool TYPE STANDARD TABLE OF textpool,
        wl_textpool TYPE textpool.

*-- Read the Entire Textpool into an Internal table
  READ TEXTPOOL sy-repid INTO tl_textpool LANGUAGE sy-langu.
  IF sy-subrc IS INITIAL.
    SORT tl_textpool BY id key.
  ENDIF.

*-- Font start
  CLEAR wa_contents.
  wa_contents-line c_style_start.
  APPEND wa_contents TO t_contents.

*-- New line
  CLEAR wa_contents.
  wa_contents-line c_new_line.
  APPEND wa_contents TO t_contents.

*-- Program name : Email Attachment
  CLEARwl_textpoolwa_contents.
  READ TABLE tl_textpool INTO wl_textpool
                         WITH KEY id 'I' key 'T03'
                         BINARY SEARCH.
  IF sy-subrc EQ 0.
    wa_contents-line wl_textpool-entry.
*-- "#" Present in the Text Element will be replaced by the below value
    REPLACE'#' WITH 'Email Attachment' INTO wa_contents-line.
    APPEND wa_contents TO t_contents.
  ENDIF.

*-- New line
  CLEAR wa_contents.
  wa_contents-line c_new_line.
  APPEND wa_contents TO t_contents.
*-- New line
  CLEAR wa_contents.
  wa_contents-line c_new_line.
  APPEND wa_contents TO t_contents.

*-- You can also change or add the text here...
  CLEARwl_textpoolwa_contents.
  READ TABLE tl_textpool INTO wl_textpool
                         WITH KEY id 'I' key 'T04'
                         BINARY SEARCH.
  IF sy-subrc EQ 0.
    wa_contents-line wl_textpool-entry.
    APPEND wa_contents TO t_contents.
  ENDIF.

*-- New line
  CLEAR wa_contents.
  wa_contents-line c_new_line.
  APPEND wa_contents TO t_contents.
*-- New line
  CLEAR wa_contents.
  wa_contents-line c_new_line.
  APPEND wa_contents TO t_contents.

*-- For giving spaces between texts, you can use...
  CLEARwl_textpoolwa_contents.
  READ TABLE tl_textpool INTO wl_textpool
                         WITH KEY id 'I' key 'T05'
                         BINARY SEARCH.
  IF sy-subrc EQ 0.
    wa_contents-line wl_textpool-entry.
    APPEND wa_contents TO t_contents.
  ENDIF.

*-- New line
  CLEAR wa_contents.
  wa_contents-line c_new_line.
  APPEND wa_contents TO t_contents.

*-- For Ex; Employee Numbers :
  CLEARwl_textpoolwa_contents.
  READ TABLE tl_textpool INTO wl_textpool
                         WITH KEY id 'I' key 'T06'
                         BINARY SEARCH.
  IF sy-subrc EQ 0.
    wa_contents-line wl_textpool-entry.
*-- How to give Spaces in between texts
    CONCATENATE wa_contents-line c_space c_emp1 c_space c_emp2
                                 c_space c_emp3 INTO wa_contents-line.
    APPEND wa_contents TO t_contents.
  ENDIF.
*-- New line
  CLEAR wa_contents.
  wa_contents-line c_new_line.
  APPEND wa_contents TO t_contents.
*-- New line
  CLEAR wa_contents.
  wa_contents-line c_new_line.
  APPEND wa_contents TO t_contents.

*-- For more Information on HTML..
  CLEARwl_textpoolwa_contents.
  READ TABLE tl_textpool INTO wl_textpool
                         WITH KEY id 'I' key 'T07'
                         BINARY SEARCH.
  IF sy-subrc EQ 0.
    wa_contents-line wl_textpool-entry.
    APPEND wa_contents TO t_contents.
  ENDIF.

*-- New line
  CLEAR wa_contents.
  wa_contents-line c_new_line.
  APPEND wa_contents TO t_contents.
*-- New line
  CLEAR wa_contents.
  wa_contents-line c_new_line.
  APPEND wa_contents TO t_contents.
*-- Subject of the Mail
  CONCATENATE text-t08 w_mail_subj INTO w_mail_subj.

ENDFORM.                    " frame_mail_body
*&---------------------------------------------------------------------*
*& Form Name     : Send_mail                                           *
*&---------------------------------------------------------------------*
*& Description   : To set the recipients and send the mail             *
*&---------------------------------------------------------------------*
*& Parameters    : None                                                *
*&---------------------------------------------------------------------*
FORM send_mail.

*-- Local data declaration for sending mail
  DATAl_send_request  TYPE REF TO cl_bcs,
        l_document      TYPE REF TO cl_document_bcs,
        l_sender        TYPE REF TO cl_sapuser_bcs,
        l_sub           TYPE char50,
        l_recipient     TYPE REF TO if_recipient_bcs,
        tl_contents     TYPE STANDARD TABLE OF soli,
        l_doc_len       TYPE so_obj_len,
        l_cnt           TYPE sy-tabix,
        l_rcv_email     TYPE adr6-smtp_addr,
        l_result        TYPE sy-binpt,
        l_bcs_exception TYPE REF TO cx_bcs,
        l_subj          TYPE string,
        wl_mailid       LIKE LINE OF s_mailid.

  TRY.
*-- Create persistent send request
      l_send_request cl_bcs=>create_persistent).
      tl_contents[] t_contents[].

*-- Get the length of the Document
      DESCRIBE TABLE tl_contents LINES l_cnt.
      READ TABLE tl_contents INTO wa_contents INDEX l_cnt.
      l_doc_len l_cnt 255 STRLENwa_contents ).
*-- Subject of the mail
      l_sub w_mail_subj.

*-- Create Document
      l_document cl_document_bcs=>create_document(
                   i_type       c_htm
                   i_text       tl_contents
                   i_length     l_doc_len
                   i_subject    l_sub
                   i_language   sy-langu
                   i_importance '1' ).
*-- Subject of the mail
      MOVE w_mail_subj TO l_subj.
      w_document l_document.

      TRY.
*-- Set the Message Subject
          CALL METHOD l_send_request->set_message_subject
            EXPORTING
              ip_subject l_subj.
        CATCH cx_sy_dyn_call_illegal_method.
      ENDTRY.

*-- Add document to send request
      CALL METHOD l_send_request->set_documentl_document ).

*-- Do send delivery info for successful mails
      CALL METHOD l_send_request->set_status_attributes
        EXPORTING
          i_requested_status 'E'
          i_status_mail      'A'.

*-- Set sender
      l_sender cl_sapuser_bcs=>createsy-uname ).
      CALL METHOD l_send_request->set_sender
        EXPORTING
          i_sender l_sender.

*-- To frame the attachments for the mail
      PERFORM frame_attachments.
*-- Add the recipients to the Send mail
      LOOP AT s_mailid INTO wl_mailid.
        l_rcv_email wl_mailid-low.

        CHECK NOT l_rcv_email IS INITIAL.
        l_recipient cl_cam_address_bcs=>create_internet_address(
                                                      l_rcv_email ).
        CALL METHOD l_send_request->add_recipient
          EXPORTING
            i_recipient l_recipient
            i_express   'X'.
      ENDLOOP.

*-- Send Email
      CALL METHOD l_send_request->send(
          EXPORTING
            i_with_error_screen 'X'
          RECEIVING
            result              l_result ).

      IF l_result 'X'.
        MESSAGE s999(zzWITH
        'Mail Sent Successfully'(003).
      ENDIF.

    CATCH cx_bcs INTO l_bcs_exception.
      IF l_result NE 'X'.
        MESSAGE s999(zzWITH
        'Mail Not Successful'(004).
      ENDIF.
  ENDTRY.
  COMMIT WORK.                                             "Commit Work

ENDFORM.                    " send_mail
*&---------------------------------------------------------------------*
*& Form Name     : frame_attachments                                   *
*&---------------------------------------------------------------------*
*& Description   : To frame the attachments for the mail to be sent    *
*&---------------------------------------------------------------------*
*& Parameters    : None                                                *
*&---------------------------------------------------------------------*
FORM frame_attachments.
*-- Local Data declaration
  DATAl_subject   TYPE so_obj_des,
        l_att_type  TYPE soodk-objtp.

*-- Subject of the Attachment
  l_subject  w_file.
*-- Format of the Attachment
  l_att_type w_extn.

  IF t_mailhex[] IS NOT INITIAL.
    TRY.
*-- Add Attachment to the Document
        CALL METHOD w_document->add_attachment
          EXPORTING
            i_attachment_type    l_att_type
            i_attachment_subject l_subject
            i_att_content_hex    t_mailhex.

      CATCH cx_document_bcs.
    ENDTRY.
  ENDIF.
ENDFORM.                    " frame_attachments

input:

output:


















No comments:

Post a Comment