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: 

excel sheet as an attachment to the mail box

Former Member
0 Kudos

Hi,

i need to send an excel sheet as an attachement to the outlook mail box..

is any function module is there which serves my requirement? or if any other way is there pls let me know.

thanks..

5 REPLIES 5

Former Member
0 Kudos

hi,

try this FM 'SO_NEW_DOCUMENT_ATT_SEND_API1'

theja

Former Member
0 Kudos

Hi Mahesh,

Check this

Cheers

VJ

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Here is some sample code.



report zrich_0001.

parameters: p_email   type somlreci1-receiver
                               default you@yourcompany.com'.

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.


Regards,

Rich Heilman

0 Kudos

Sending an excel spreadsheet attachment on an E-Mail .

Afternoon,

Based upon program BCS_EXAMPLE_5 I have created a program that generates an e-mail and creates a spreadsheet attachment. The problem I have is how do get the contents of an internal table into the spreadsheet attachment?

My attempt at resolving the problem has been as follows:-

I have declared a variable document which points to the class cl_document_bcs. I have then instantiated the variable using a static method. I then call the method add_attachment (document->add_attachment) and pass the text content which contains two lines of text.

This creates a spreadsheet will the two lines of text in the first cell.

Could I use the binary content instead? If so how do I declare a variable that has the same type as a work area of binary content because this has a data type of RAW.

Any assistance would be very much appreciated. A copy of my code can be seen below:-

Thanks and regards

hanks.

Former Member
0 Kudos

Hi,

Check this blog it depends on the release you're on if you can use it.

/people/eddy.declercq/blog/2006/04/03/unknown-thus-unloved

I wrote an additonal FM to be able to insert table data as excel sheet in the mail. The zemail_document definitioon can be retrieved from above blog.

FUNCTION Z_CREATE_XLS_DOC .

*"----


""Local interface:

*" IMPORTING

*" REFERENCE(ATT_SUBJECT)

*" EXPORTING

*" REFERENCE(ATTACHMENT) TYPE ZEMAIL_DOCUMENT

*" TABLES

*" T_DATA

*"----


data : descr_ref type ref to cl_abap_structdescr.

data : field(30) type c.

data : str type ty_c255.

data : ls_soli type line of soli_tab,

ls_soli_new type line of soli_tab,

lt_soli type soli_tab.

field-symbols: <ac> type abap_compdescr,

<data> type any,

<fieldval> type any.

descr_ref ?= cl_abap_typedescr=>describe_by_data( t_data ).

  • Process dynamic table

loop at t_data assigning <data>.

  • Process components of table

loop at descr_ref->components assigning <ac>.

concatenate '<data>-' <ac>-name into field.

assign (field) to <fieldval>.

write <fieldval> to str.

  • Attach to excel include for mailing usin a comma as seperator

PERFORM add_field_to_msg USING 'X'

str

changing ls_soli

ls_soli_new.

if not ls_soli_new is initial.

append ls_soli to lt_soli.

ls_soli = ls_soli_new.

clear ls_soli_new.

endif.

endloop.

write crlf to str.

PERFORM add_field_to_msg USING ' '

str

changing ls_soli

ls_soli_new

.

endloop.

if not ls_soli_new is initial.

append ls_soli to lt_soli.

ls_soli = ls_soli_new.

clear ls_soli_new.

endif.

append ls_soli to lt_soli.

attachment-SUBJECT = att_subject.

attachment-TYPE = 'CSV'.

append lines of lt_soli to attachment-content_text.

ENDFUNCTION.

FORM add_field_to_msg USING comma type c

p_fld type ty_c255

changing p_soli type soli

p_soli_new type soli.

field-symbols <soli> type soli.

data : ls_soli type soli,

l_len1 type i,

l_len2 type i,

l_Len3 type i.

  • Calculate current string length

l_len1 = strlen( p_soli ).

  • Calculate length of string to be added

condense p_fld.

l_len2 = strlen( p_fld ).

  • Decide new line to be used

l_len3 = l_len1 + l_len2 + 2. "encapsulating "" need to be counted too

if l_len3 > max_line_size.

assign p_soli_new to <soli>.

else.

assign p_soli to <soli>.

endif.

if not p_fld = crlf.

if comma = 'X'.

concatenate <soli> ',' '"' p_fld '"' into <soli>.

else.

concatenate <soli> '"' p_fld '"' into <soli>.

endif.

else.

concatenate <soli> ',' p_fld into <soli>.

endif.

ENDFORM. " add field to form

<a href="/people/eddy.declercq/blog/2006/04/03/unknown-thus-unloved:///people/eddy.declercq/blog/2006/04/03/unknown-thus-unloved