Skip to Content
0
Jan 14, 2023 at 10:16 PM

.xls to .xlsx convert error while creation of attachement during sending mail

396 Views Last edit Jan 15, 2023 at 10:06 AM 2 rev

Hi Everyone ,

Getting the Excel format error while to make the attachment from .xls to .xlsx , please help :

CONSTANTS: c_tab(1) TYPE c VALUE
cl_abap_char_utilities=>horizontal_tab,
" Tab Character
c_cr(1) TYPE c VALUE cl_abap_char_utilities=>cr_lf,
" Line Feed for End-Of_line
c_ext TYPE soodk-objtp VALUE 'XLS'. " XLS Extension
DATA: l_text TYPE char1024. " Text content for mail attachment
* Preparing body of the Mail
MOVE 'Please find the Error Messages attached' TO l_text.
APPEND l_text TO i_content.
FIELD-SYMBOLS: <lfs_table>, " Internal table structure
<lfs_con>. " Field Content

DATA: l_con(50) TYPE c. " Field Content in character format

* Columns to be tab delimeted
LOOP AT gt_output1 ASSIGNING <lfs_table>.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <lfs_table>
TO <lfs_con>.
IF sy-subrc NE 0.
CONCATENATE c_cr l_text INTO l_text.
APPEND l_text TO i_attach.
EXIT.
ELSE.
CLEAR: l_con.
MOVE <lfs_con> TO l_con.
CONDENSE l_con.
IF sy-index = 1.
CLEAR: l_text.
MOVE l_con TO l_text.
ELSE.
CONCATENATE l_text l_con INTO l_text
SEPARATED BY c_tab.
ENDIF.
ENDIF.
ENDDO.
ENDLOOP.
*
DATA: li_solix TYPE solix_tab .
*try.
*
*cl_bcs_convert=>string_to_solix(
*
*exporting
*
*iv_string = i_attach
*
*importing
*
*et_solix = li_solix ).
*
** ev_size = size ).
*
*catch cx_bcs.
*
*message e445(so).
*
*endtry.
DATA : lv_emp_string TYPE string.
DATA : lv_emp_xstring TYPE xstring.
DATA : size TYPE so_obj_len.
LOOP AT i_attach INTO DATA(wa_attach). .

CONCATENATE lv_emp_string

wa_attach-line cl_abap_char_utilities=>horizontal_tab

cl_abap_char_utilities=>newline

INTO lv_emp_string.

ENDLOOP.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

EXPORTING

text = lv_emp_string

IMPORTING

buffer = lv_emp_xstring.

CALL METHOD cl_document_bcs=>xstring_to_solix
EXPORTING
ip_xstring = lv_emp_xstring
receiving
rt_solix = li_solix.
DATA : lv_size TYPE c LENGTH 12.
lv_size = XSTRLEN( lv_emp_xstring ).
*cl_bcs_convert=>string_to_solix(
*
*EXPORTING
*
*iv_string = lv_emp_string
*
*iv_codepage = '4103' "suitable for MS Excel, leave empty
*
*iv_add_bom = 'X' "for other doc types
*
*IMPORTING
*
*et_solix = li_solix
*
*ev_size = size ).

* Creates persistent send request
TRY.
l_send_request = cl_bcs=>create_persistent( ).

* Creating Document
l_document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = i_content[]
i_subject = 'Material Master Error' ).

* Preparing contents of attachment with Change Log
DESCRIBE TABLE gt_output1 LINES l_lines.

* Size to multiplied by 2 for UNICODE enabled systems
l_size = l_lines * 2 * 255.
DATA lt_att_head TYPE soli_tab.
DATA lv_filename TYPE string.
DATA lv_text_line TYPE soli.
lv_filename = 'MatError.XLSX'. CONCATENATE '&SO_FILENAME=' lv_filename INTO lv_text_line.
APPEND lv_text_line TO lt_att_head.
* Adding Attachment
CALL METHOD l_document->add_attachment
EXPORTING
i_attachment_type = 'XLS'
i_attachment_size = lv_size
i_attachment_subject = 'Material Master Error'
i_att_content_hex = li_solix
i_attachment_header = lt_att_head.

* Add document to send request
CALL METHOD l_send_request->set_document( l_document ).

* Get Sender Object
l_uname = sy-uname.
l_sender = cl_sapuser_bcs=>create( l_uname ).
CALL METHOD l_send_request->set_sender
EXPORTING
i_sender = l_sender.

* E-Mail
TRANSLATE p_lv_mailid TO LOWER CASE.
l_recipient = cl_cam_address_bcs=>create_internet_address( p_lv_mailid )
.
CALL METHOD l_send_request->add_recipient
EXPORTING
i_recipient = l_recipient
i_express = 'U'
i_copy = ' '
i_blind_copy = ' '
i_no_forward = ' '.

*Trigger E-Mail immediately
l_send_request->set_send_immediately( 'X' ).
CALL METHOD l_send_request->send( ).
COMMIT WORK.
CATCH cx_document_bcs INTO l_bcs_exception.
CATCH cx_send_req_bcs INTO l_send_exception.
CATCH cx_address_bcs INTO l_addr_exception.
ENDTRY.