Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in sending attachment using "SO_NEW_DOCUMENT_ATT_SEND_API1"

Former Member
0 Kudos
160

Hi All,

I have a requirement of sending the data available in an internal table via email. I need to populate this content into excel sheet and send it as an attachment to recipients. I am using the following code for that.

REPORT z51515t_doc_att.

DATA: objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE.

DATA: objhead LIKE solisti1 OCCURS 1 WITH HEADER LINE.

DATA: objbin LIKE solisti1 OCCURS 10 WITH HEADER LINE.

DATA: objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE.

DATA: reclist LIKE somlreci1 OCCURS 5 WITH HEADER LINE.

DATA: doc_chng LIKE sodocchgi1.

DATA: tab_lines LIKE sy-tabix.

DATA : BEGIN OF itab OCCURS 0,

pernr LIKE p0001-pernr,

name LIKE p0002-vorna,

END OF itab.

  • Creation of the document to be sent

  • File Name

doc_chng-obj_name = 'SENDFILE'.

  • Mail Subject

doc_chng-obj_descr = 'Delivered Mail'.

  • Mail Contents

objtxt = 'My Content 1 '.

APPEND objtxt.

objtxt = 'My Content2'.

APPEND objtxt.

DESCRIBE TABLE objtxt LINES tab_lines.

    • Creation of the entry for the document

CLEAR objpack-transf_bin. "Will get content from content_text

objpack-head_start = 1. "Reads given row number in object header

objpack-head_num = 0.

objpack-body_start = 1.

objpack-body_num = tab_lines.

objpack-doc_type = 'RAW'.

APPEND objpack.

  • Prepare content for attachment

DO 3 TIMES.

itab-pernr = 182.

itab-name = 'Prasath'.

APPEND itab.

itab-pernr = 1000.

itab-name = 'Praveen'.

APPEND itab.

ENDDO.

  • Populate attachment content

LOOP AT itab.

CONCATENATE itab-pernr itab-name INTO objbin-line.

APPEND objbin.

ENDLOOP.

DESCRIBE TABLE objbin LINES tab_lines.

    • Creation of the entry for the compressed attachment

objpack-transf_bin = 'X'. "Will get content from content_bin

objpack-head_start = 1.

objpack-head_num = 1.

objpack-body_start = 1.

objpack-body_num = tab_lines.

objpack-doc_type = 'EXT'.

objpack-obj_name = 'WEBSITE'.

objpack-obj_descr = 'MyFile.XLS'.

objpack-doc_size = tab_lines * 255.

APPEND objpack.

  • target recipent

CLEAR reclist.

reclist-receiver = 'test@here.com'.

reclist-express = 'X'.

reclist-rec_type = 'U'.

APPEND reclist.

  • copy recipents

CLEAR reclist.

reclist-receiver = 'secondtest@here.com'.

reclist-express = 'X'.

reclist-rec_type = 'U'.

reclist-copy = 'X'.

APPEND reclist.

  • Sending the document

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = doc_chng

put_in_outbox = 'X'

commit_work = 'X'

TABLES

packing_list = objpack

object_header = objhead

contents_bin = objbin

contents_txt = objtxt

receivers = reclist

EXCEPTIONS

too_many_receivers = 1

document_not_sent = 2

operation_no_authorization = 4

OTHERS = 99.

Now using this code i am able to send out the file but the data in the XL file is not aligned properly. When i open the attachment that i receive the contents of itab appear inside the same cell - A1 of the XL sheet. I want the data to be available in different rows and columns. How do i achieve this?

Thanks,

Prasath N

1 ACCEPTED SOLUTION

Former Member
0 Kudos
113

Hi Prasath ,

try this in your code

CONCATENATE itab-pernr itab-name INTO objbin-line

SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB

While downloading to XL it will separate the columns

Regards

Rajesh.D

8 REPLIES 8

Former Member
0 Kudos
113

Hi Prasath,

Try to use the concatenate statment to Internal Table <b>objbin</b> by a Tab Delimiter. then the values will be deisplayed in Each cell based on the no. of columns.

Regards,

Anbalagan

0 Kudos
113

Hi Anbalagan,

Thanks got your quick response. Can you let me know the addition/statement that i should use to include the tab delimiter. In CONCATENATE statement using F1 help i can only find the addition "SEPERATED BY SPACE". I am not aware how to include a tab space here. Can you help me out.

Thanks,

Prasath N

Former Member
0 Kudos
113

Hi Prashant ,

for this use CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB as separator. You will be able to get data in different columns.

Regards

Rajesh

Former Member
0 Kudos
113

Hi Prasath,

CONSTANTS: c_tab TYPE x VALUE '09'.

While concatenatinng the fld to Internal table use the below statement

CONCATENATE itab-pernr itab-name INTO objbin-line separated by c_tab.

Regards,

Anbalagan

Former Member
0 Kudos
113

See the below program it was sending the file as attach in XLS format by column and rows are properly placed ... see the

<b>Code in my program :

CONSTANTS: con_cret TYPE x VALUE '0D', "OK for non Unicode

con_tab TYPE x VALUE '09'. "OK for non Unicode</b>

REPORT  ZEMAIL_ATTACH                   .
TABLES: ekko.

PARAMETERS: p_email   TYPE somlreci1-receiver
                                  DEFAULT 'loganthan.girishkumar@tcs.com'.

TYPES: BEGIN OF t_ekpo,
  ebeln TYPE ekpo-ebeln,
  ebelp TYPE ekpo-ebelp,
  aedat TYPE ekpo-aedat,
  matnr TYPE ekpo-matnr,
 END OF t_ekpo.
DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0,
      wa_ekpo TYPE t_ekpo.

TYPES: BEGIN OF t_charekpo,
  ebeln(10) TYPE c,
  ebelp(5)  TYPE c,
  aedat(8)  TYPE c,
  matnr(18) TYPE c,
 END OF t_charekpo.
DATA: wa_charekpo TYPE t_charekpo.

DATA:   it_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
                WITH HEADER LINE.
DATA:   it_attach TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
                WITH HEADER LINE.

DATA:   t_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
        t_contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,
        t_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
        t_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,
        t_object_header LIKE solisti1 OCCURS 0 WITH HEADER LINE,
        w_cnt TYPE i,
        w_sent_all(1) TYPE c,
        w_doc_data LIKE sodocchgi1,
        gd_error    TYPE sy-subrc,
        gd_reciever TYPE sy-subrc.


************************************************************************
*START_OF_SELECTION
START-OF-SELECTION.
*   Retrieve sample data from table ekpo
  PERFORM data_retrieval.

*   Populate table with detaisl to be entered into .xls file
  PERFORM build_xls_data_table.


************************************************************************
*END-OF-SELECTION
END-OF-SELECTION.
* Populate message body text
  perform populate_email_message_body.

* Send file by email as .xls speadsheet
  PERFORM send_file_as_email_attachment
                               tables it_message
                                      it_attach
                                using p_email
                                      'Example .xls documnet attachment'
                                      'XLS'
                                      'filename'
                                      ' '
                                      ' '
                                      ' '
                             changing gd_error
                                      gd_reciever.

*   Instructs mail send program for SAPCONNECT to send email(rsconn01)
  PERFORM initiate_mail_execute_program.


*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM data_retrieval.
  SELECT ebeln ebelp aedat matnr
   UP TO 10 ROWS
    FROM ekpo
    INTO TABLE it_ekpo.
ENDFORM.                    " DATA_RETRIEVAL


*&---------------------------------------------------------------------*
*&      Form  BUILD_XLS_DATA_TABLE
*&---------------------------------------------------------------------*
*       Build data table for .xls document
*----------------------------------------------------------------------*
FORM build_xls_data_table.
  CONSTANTS: con_cret TYPE x VALUE '0D',  "OK for non Unicode
             con_tab TYPE x VALUE '09'.   "OK for non Unicode

*If you have Unicode check active in program attributes thnen you will
*need to declare constants as follows
*class cl_abap_char_utilities definition load.
*constants:
*    con_tab  type c value cl_abap_char_utilities=>HORIZONTAL_TAB,
*    con_cret type c value cl_abap_char_utilities=>CR_LF.


  CONCATENATE 'EBELN' 'EBELP' 'AEDAT' 'MATNR'
         INTO it_attach SEPARATED BY con_tab.
  CONCATENATE con_cret it_attach  INTO it_attach.
  APPEND  it_attach.

  LOOP AT it_ekpo INTO wa_charekpo.
    CONCATENATE wa_charekpo-ebeln wa_charekpo-ebelp
                wa_charekpo-aedat wa_charekpo-matnr
           INTO it_attach SEPARATED BY con_tab.
    CONCATENATE con_cret it_attach  INTO it_attach.
    APPEND  it_attach.
  ENDLOOP.
ENDFORM.                    " BUILD_XLS_DATA_TABLE


*&---------------------------------------------------------------------*
*&      Form  SEND_FILE_AS_EMAIL_ATTACHMENT
*&---------------------------------------------------------------------*
*       Send email
*----------------------------------------------------------------------*
FORM send_file_as_email_attachment tables pit_message
                                          pit_attach
                                    using p_email
                                          p_mtitle
                                          p_format
                                          p_filename
                                          p_attdescription
                                          p_sender_address
                                          p_sender_addres_type
                                 changing p_error
                                          p_reciever.


  DATA: ld_error    TYPE sy-subrc,
        ld_reciever TYPE sy-subrc,
        ld_mtitle LIKE sodocchgi1-obj_descr,
        ld_email LIKE  somlreci1-receiver,
        ld_format TYPE  so_obj_tp ,
        ld_attdescription TYPE  so_obj_nam ,
        ld_attfilename TYPE  so_obj_des ,
        ld_sender_address LIKE  soextreci1-receiver,
        ld_sender_address_type LIKE  soextreci1-adr_typ,
        ld_receiver LIKE  sy-subrc.

  ld_email   = p_email.
  ld_mtitle = p_mtitle.
  ld_format              = p_format.
  ld_attdescription      = p_attdescription.
  ld_attfilename         = p_filename.
  ld_sender_address      = p_sender_address.
  ld_sender_address_type = p_sender_addres_type.


* Fill the document data.
  w_doc_data-doc_size = 1.

* Populate the subject/generic message attributes
  w_doc_data-obj_langu = sy-langu.
  w_doc_data-obj_name  = 'SAPRPT'.
  w_doc_data-obj_descr = ld_mtitle .
  w_doc_data-sensitivty = 'F'.

* Fill the document data and get size of attachment
  CLEAR w_doc_data.
  READ TABLE it_attach INDEX w_cnt.
  w_doc_data-doc_size =
     ( w_cnt - 1 ) * 255 + STRLEN( it_attach ).
  w_doc_data-obj_langu  = sy-langu.
  w_doc_data-obj_name   = 'SAPRPT'.
  w_doc_data-obj_descr  = ld_mtitle.
  w_doc_data-sensitivty = 'F'.
  CLEAR t_attachment.
  REFRESH t_attachment.
  t_attachment[] = pit_attach[].

* Describe the body of the message
  CLEAR t_packing_list.
  REFRESH t_packing_list.
  t_packing_list-transf_bin = space.
  t_packing_list-head_start = 1.
  t_packing_list-head_num = 0.
  t_packing_list-body_start = 1.
  DESCRIBE TABLE it_message LINES t_packing_list-body_num.
  t_packing_list-doc_type = 'RAW'.
  APPEND t_packing_list.

* Create attachment notification
  t_packing_list-transf_bin = 'X'.
  t_packing_list-head_start = 1.
  t_packing_list-head_num   = 1.
  t_packing_list-body_start = 1.

  DESCRIBE TABLE t_attachment LINES t_packing_list-body_num.
  t_packing_list-doc_type   =  ld_format.
  t_packing_list-obj_descr  =  ld_attdescription.
  t_packing_list-obj_name   =  ld_attfilename.
  t_packing_list-doc_size   =  t_packing_list-body_num * 255.
  APPEND t_packing_list.

* Add the recipients email address
  CLEAR t_receivers.
  REFRESH t_receivers.
  t_receivers-receiver = ld_email.
  t_receivers-rec_type = 'U'.
  t_receivers-com_type = 'INT'.
  t_receivers-notif_del = 'X'.
  t_receivers-notif_ndel = 'X'.
  APPEND t_receivers.

  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
       EXPORTING
            document_data              = w_doc_data
            put_in_outbox              = 'X'
            sender_address             = ld_sender_address
            sender_address_type        = ld_sender_address_type
            commit_work                = 'X'
       IMPORTING
            sent_to_all                = w_sent_all
       TABLES
            packing_list               = t_packing_list
            contents_bin               = t_attachment
            contents_txt               = it_message
            receivers                  = t_receivers
       EXCEPTIONS
            too_many_receivers         = 1
            document_not_sent          = 2
            document_type_not_exist    = 3
            operation_no_authorization = 4
            parameter_error            = 5
            x_error                    = 6
            enqueue_error              = 7
            OTHERS                     = 8.

* Populate zerror return code
  ld_error = sy-subrc.

* Populate zreceiver return code
  LOOP AT t_receivers.
    ld_receiver = t_receivers-retrn_code.
  ENDLOOP.
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  INITIATE_MAIL_EXECUTE_PROGRAM
*&---------------------------------------------------------------------*
*       Instructs mail send program for SAPCONNECT to send email.
*----------------------------------------------------------------------*
FORM initiate_mail_execute_program.
  WAIT UP TO 2 SECONDS.
  SUBMIT rsconn01 WITH mode = 'INT'
                WITH output = 'X'
                AND RETURN.
ENDFORM.                    " INITIATE_MAIL_EXECUTE_PROGRAM


*&---------------------------------------------------------------------*
*&      Form  POPULATE_EMAIL_MESSAGE_BODY
*&---------------------------------------------------------------------*
*        Populate message body text
*----------------------------------------------------------------------*
form populate_email_message_body.
  REFRESH it_message.
  it_message = 'Please find attached a list test ekpo records'.
  APPEND it_message.
endform.                    " POPULATE_EMAIL_MESSAGE_BODY


reward points if it is usefull ....

Girish

0 Kudos
113

Hi

Thanks for your replies. I tried adding the following statements,

DATA : c_tab TYPE x VALUE '09'.

..................

..................

..................

CONCATENATE itab-pernr itab-name INTO objbin-line SEPARATED BY c_tab.

.................

.................

But it says that it needs C_TAB in a different format. I get the following ysntax error on adding the above statement.

<b>C_TAB must be of character type object (data type C,N,D,T or STRING)</b>

Any inputs on how to pass the tab in a string or other formats?

Thanks,

Prasath N

Former Member
0 Kudos
114

Hi Prasath ,

try this in your code

CONCATENATE itab-pernr itab-name INTO objbin-line

SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB

While downloading to XL it will separate the columns

Regards

Rajesh.D

0 Kudos
113

Thanks Rajesh.

This worked perfectly.

Thanks,

Prasath N