Skip to Content
author's profile photo Former Member
Former Member

Send an Internal Table as an excel attachment via email

Hello Experts,

I would like to know if somebody can give me some ideas in order to send an internal table as an excel attachment file via email.

Thanks in advance,

-C

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 04:09 AM

    Hi,

    data: p_email type somlreci1-receiver

    value 'Give Email ID.

    data: begin of it001 occurs 0,

    bukrs type t001-bukrs,

    butxt type t001-butxt,

    end of it001.

    data: imessage type standard table of solisti1 with header line,

    iattach type standard table of solisti1 with header line,

    ipacking_list like sopcklsti1 occurs 0 with header line,

    ireceivers like somlreci1 occurs 0 with header line,

    iattachment like solisti1 occurs 0 with header line.

    start-of-selection.

    select bukrs butxt into table it001 from t001.

    • Populate table with detaisl to be entered into .xls file

    perform build_xls_data .

    • Populate message body text

    clear imessage. refresh imessage.

    imessage = 'Please find attached excel file'.

    append imessage.

    • Send file by email as .xls speadsheet

    perform send_email_with_xls tables imessage

    iattach

    using p_email

    'Example Excel Attachment'

    'XLS'

    'TestFileName'

    'CompanyCodes'.

    ************************************************************************

    • Form BUILD_XLS_DATA

    ************************************************************************

    form build_xls_data .

    • 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 'BUKRS' 'BUTXT'

    into iattach separated by con_tab.

    concatenate con_cret iattach into iattach.

    append iattach.

    loop at it001 .

    concatenate it001-bukrs it001-butxt

    into iattach separated by con_tab.

    concatenate con_cret iattach into iattach.

    append iattach.

    endloop .

    endform.

    ************************************************************************

    • Form SEND_EMAIL_WITH_XLS

    ************************************************************************

    form send_email_with_xls tables pit_message

    pit_attach

    using p_email

    p_mtitle

    p_format

    p_filename

    p_attdescription.

    data: xdocdata like sodocchgi1,

    xcnt type i.

    • Fill the document data.

    xdocdata-doc_size = 1.

    • Populate the subject/generic message attributes

    xdocdata-obj_langu = sy-langu .

    xdocdata-obj_name = 'SAPRPT' .

    xdocdata-obj_descr = p_mtitle .

    • Fill the document data and get size of attachment

    clear xdocdata.

    read table iattach index xcnt.

    xdocdata-doc_size =

    ( xcnt - 1 ) * 255 + strlen( iattach ).

    xdocdata-obj_langu = sy-langu.

    xdocdata-obj_name = 'SAPRPT'.

    xdocdata-obj_descr = p_mtitle.

    clear iattachment. refresh iattachment.

    iattachment[] = pit_attach[].

    • Describe the body of the message

    clear ipacking_list. refresh ipacking_list.

    ipacking_list-transf_bin = space.

    ipacking_list-head_start = 1.

    ipacking_list-head_num = 0.

    ipacking_list-body_start = 1.

    describe table imessage lines ipacking_list-body_num.

    ipacking_list-doc_type = 'RAW'.

    append ipacking_list.

    • Create attachment notification

    ipacking_list-transf_bin = 'X'.

    ipacking_list-head_start = 1.

    ipacking_list-head_num = 1.

    ipacking_list-body_start = 1.

    describe table iattachment lines ipacking_list-body_num.

    ipacking_list-doc_type = p_format.

    ipacking_list-obj_descr = p_attdescription.

    ipacking_list-obj_name = p_filename.

    ipacking_list-doc_size = ipacking_list-body_num * 255.

    append ipacking_list.

    • Add the recipients email address

    clear ireceivers. refresh ireceivers.

    ireceivers-receiver = p_email.

    ireceivers-rec_type = 'U'.

    ireceivers-com_type = 'INT'.

    ireceivers-notif_del = 'X'.

    ireceivers-notif_ndel = 'X'.

    append ireceivers.

    call function 'SO_DOCUMENT_SEND_API1'

    exporting

    document_data = xdocdata

    put_in_outbox = 'X'

    commit_work = 'X'

    tables

    packing_list = ipacking_list

    contents_bin = iattachment

    contents_txt = imessage

    receivers = ireceivers

    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.

    endform.

    Reward if useful....

    Thanks,

    Durai.V

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hello Durai,

      I sample your method. I did received the email attached as .xls format. BUT, when opening the file Microsoft Excel is not able to open it and the following error message is displayed: "The file is not in a recognizable format"

      Do you happen to know what could be causing this?

      Thanks again!

      -C

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 04:56 AM

    Hi Carlos,

    After getting data into your internal table (i_output inthis case),

    just write the following form.

    FORM send_mail .

    DATA: lwa_fieldcat2 TYPE slis_fieldcat_alv,

    lwa_fieldcat TYPE slis_fieldcat_alv,

    l_cnt TYPE i,

    l_any TYPE string,

    l_fname LIKE lwa_fieldcat2-fieldname,

    l_obj_mail_title(100) TYPE c ,

    l_obj_descrp(100) TYPE c,

    l_string(4080) TYPE c,

    l_strings(4080) TYPE c,

    l_cn TYPE i,

    l_ci TYPE i,

    l_rem TYPE i,

    l_diff TYPE i,

    l_cns TYPE i,

    l_cis TYPE i,

    l_cx TYPE i,

    l_cxs TYPE i,

    l_tab_lines LIKE sy-tabix,

    l_date(10) TYPE c,

    l_time(5) TYPE c.

    CONSTANTS : lc_00 VALUE '0',

    lc_01 VALUE '1',

    lc_raw(3) TYPE c VALUE 'RAW'.

    FIELD-SYMBOLS: <fs> TYPE ANY.

    l_obj_mail_title = ''.

    • IF sy-sysid NE lc_id.

    • CONCATENATE text-036 sy-sysid INTO l_obj_mail_title.

    • ELSE.

    WRITE sy-datum TO l_date DD/MM/YYYY.

    WRITE sy-uzeit TO l_time USING EDIT MASK '__:__'.

    CONCATENATE text-015 l_date l_time INTO l_obj_mail_title

    SEPARATED BY space.

    • l_obj_mail_title = text-015.

    • ENDIF.

    wa_reclist-receiver = po_mail.

    wa_reclist-rec_type = c_c.

    APPEND wa_reclist TO i_reclist.

    CLEAR : wa_reclist.

    LOOP AT i_fieldcat INTO lwa_fieldcat.

    CONCATENATE l_strings lwa_fieldcat-seltext_l INTO l_strings

    SEPARATED BY w_tab.

    CLEAR lwa_fieldcat2.

    ENDLOOP.

    l_strings = l_strings+1.

    CONDENSE l_strings.

    l_cns = STRLEN( l_strings ).

    IF l_cns GT 255.

    l_cis = l_cns DIV 255.

    l_cis = l_cis + 1.

    DO l_cis TIMES.

    MOVE: l_strings+l_cxs(255) TO wa_objtxt-line.

    APPEND wa_objtxt TO i_objtxt.

    l_cxs = l_cxs + 255.

    ENDDO.

    MOVE: l_string+l_cx(254) TO wa_objtxt-line.

    wa_objtxt-line+254(1) = w_newline.

    APPEND wa_objtxt TO i_objtxt.

    CLEAR wa_objtxt.

    ELSE.

    MOVE: l_strings TO wa_objtxt-line+0(254).

    wa_objtxt-line+254(1) = w_newline.

    APPEND wa_objtxt TO i_objtxt.

    ENDIF.

    LOOP AT i_output INTO wa_output.

    CLEAR: l_cnt,l_string, l_cx, lwa_fieldcat2.

    LOOP AT i_fieldcat INTO lwa_fieldcat.

    CLEAR: l_fname.

    l_fname = lwa_fieldcat-fieldname.

    ASSIGN COMPONENT l_fname OF STRUCTURE wa_output TO <fs>.

    l_any = <fs>.

    CONCATENATE l_string l_any INTO l_string SEPARATED BY w_tab.

    CLEAR lwa_fieldcat.

    ENDLOOP.

    l_string = l_string+1.

    CONDENSE l_string.

    l_cn = STRLEN( l_string ).

    CLEAR wa_objtxt-line.

    IF l_cn GE 254.

    l_ci = l_cn DIV 255.

    l_rem = l_cn MOD 255.

    l_diff = 255 - l_rem.

    DO l_ci TIMES.

    MOVE: l_string+l_cx(255) TO wa_objtxt-line.

    APPEND wa_objtxt TO i_objtxt.

    l_cx = l_cx + 255.

    ENDDO.

    CLEAR wa_objtxt-line.

    MOVE l_string+l_cx(255) TO wa_objtxt-line.

    IF l_diff EQ 1.

    CONCATENATE wa_objtxt-line w_tab INTO wa_objtxt-line.

    APPEND wa_objtxt TO i_objtxt.

    CLEAR wa_objtxt.

    wa_objtxt-line+254(1) = w_newline.

    APPEND wa_objtxt TO i_objtxt.

    ELSE.

    CONCATENATE wa_objtxt-line w_tab INTO wa_objtxt-line.

    wa_objtxt-line+254(1) = w_newline.

    APPEND wa_objtxt TO i_objtxt.

    ENDIF.

    ELSE.

    MOVE l_string TO wa_objtxt-line.

    CONCATENATE wa_objtxt-line w_tab INTO wa_objtxt-line.

    wa_objtxt-line+254(1) = w_newline.

    APPEND wa_objtxt TO i_objtxt.

    ENDIF.

    ENDLOOP.

    wa_doc_data-obj_descr = l_obj_mail_title.

    CLEAR wa_objpack-transf_bin.

    wa_objpack-head_start = c_01.

    wa_objpack-head_num = lc_01.

    wa_objpack-body_start = lc_00.

    wa_objpack-body_num = lc_00.

    wa_objpack-doc_type = lc_raw.

    APPEND wa_objpack TO i_objpack.

    DESCRIBE TABLE i_objtxt LINES l_tab_lines.

    l_obj_descrp = l_obj_mail_title.

    wa_objpack-transf_bin = space.

    wa_objpack-head_start = lc_01.

    wa_objpack-head_num = lc_00.

    wa_objpack-body_start = lc_01.

    wa_objpack-body_num = l_tab_lines.

    wa_objpack-doc_type = c_xls.

    wa_objpack-obj_name = c_attach.

    wa_objpack-obj_descr = l_obj_descrp.

    wa_objpack-doc_size = l_tab_lines * 255.

    APPEND wa_objpack TO i_objpack.

    IF i_reclist[] IS NOT INITIAL.

    CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

    EXPORTING

    document_data = wa_doc_data

    put_in_outbox = c_x

    commit_work = c_x

    TABLES

    packing_list = i_objpack

    contents_txt = i_objtxt

    receivers = i_reclist

    EXCEPTIONS

    too_many_receivers = 1

    document_not_sent = 2

    operation_no_authorization = 4

    OTHERS = 99.

    IF sy-subrc EQ 0.

    COMMIT WORK.

    MESSAGE 1

    ELSE.

    MESSAGE 2

    ENDIF.

    ELSE.

    MESSAGE 3

    ENDIF.

    Regards,

    Siddhesh Sanghvi

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hello Siddhesh,

      Thanks for your resonse. I sample your program but it is not recognizing the "TYPE slis_fieldcat_alv". Are we missing here any includes prior calling your routine?

      Thanks again,

      -C

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 02:42 PM

    Hi,

    1) concatenate your attachment's internal table data into a string variable say L_STRING by fileds separated by a horizontal tab and rows separated by CL_ABAP_CHAR_UTILITIES=>CR_LF.

    LOOP AT T_itab into FS_itab.

    CONCATENATE

    FS_itab-field1

    FS_itab-field2 INTO L_STRING1 SEPARATED BY CL_ABAP_CHAR_UTILITIES=>horizontal_tab.

    IF SY-TABIX NE 1.

    CONCATENATE L_STRING L_STRING1 INTO L_STRING

    SEPARATED BY

    CL_ABAP_CHAR_UTILITIES=>CR_LF.

    ELSE.

    CONCATENATE L_HEADER L_STRING1 INTO

    L_STRING1.

    L_STRING = L_STRING1.

    ENDIF. " IF SY-TABIX NE C_1

    ENDLOOP. " LOOP AT T_nhtrm INTO..

    2) Now pass that string variable containg entire attachment data to FM

    CALL FUNCTION 'SCMS_STRING_TO_FTEXT'

    EXPORTING

    TEXT = L_STRING

    • IMPORTING

    • LENGTH = LENGTH

    TABLES

    FTEXT_TAB = LT_DATA_TAB.

    3) Now fill your PACKING_LIST table parameter of mail sending FM SO_NEW_DOCUMENT_ATT_SEND_API1 as follows

    DESCRIBE TABLE LT_DATA_TAB[] LINES L_TABLINES1.

    LS_OBJPACK-TRANSF_BIN = 'X'.

    LS_OBJPACK-HEAD_START = 1.

    LS_OBJPACK-HEAD_NUM = 1.

    LS_OBJPACK-BODY_START = 1.

    LS_OBJPACK-BODY_NUM = L_TABLINES1.

    LS_OBJPACK-DOC_TYPE = 'XLS'.

    LS_OBJPACK-DOC_SIZE = L_TABLINES1 * 255.

    APPEND LS_OBJPACK TO LT_OBJPACK.

    Edited by: Phani Kanth Borra on Jun 17, 2008 4:42 PM

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2008 at 02:34 PM

    size of the file also matters.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.