{"id":106,"date":"2023-11-23T03:33:00","date_gmt":"2023-11-23T03:33:00","guid":{"rendered":"https:\/\/sapabap.dwimansolution.com\/?p=106"},"modified":"2023-11-23T03:33:00","modified_gmt":"2023-11-23T03:33:00","slug":"sap-abap-macro-1","status":"publish","type":"post","link":"https:\/\/sapabap.dwimansolution.com\/index.php\/2023\/11\/23\/sap-abap-macro-1\/","title":{"rendered":"SAP ABAP &#8211; Macro #1"},"content":{"rendered":"\n<p class=\"has-large-font-size\" style=\"font-style:normal;font-weight:700\">MACRO_UDF_DOWNLOAD<\/p>\n\n\n\n<p>Sometimes we need to <strong>download our internal table to local computer.<\/strong><\/p>\n\n\n\n<p>I got this code from old code by Fredy\u00a0Santoso. All credits to him.<\/p>\n\n\n\n<p>All we need to call this macro is 1 line like this<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>macro_udf_download filename 'ASC' itab.<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Declaration variables:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DATA: d_udf_totalrecords TYPE i,\r\n      d_udf_tab          TYPE x VALUE '09',\r\n      d_udf_subrc        LIKE sy-subrc,\r\n      d_udf_fsize        TYPE i,\r\n      d_udf_memid        LIKE sy-cprog,\r\n      d_udf_chkfl        VALUE 'X',           \" Need to check file ?\r\n      d_udf_fname        LIKE rlgrap-filename,\r\n      d_udf_drcty        LIKE pcfile-path,\r\n      d_udf_drive        LIKE pcfile-drive,\r\n      d_udf_filnm        LIKE rlgrap-filename,\r\n      d_udf_extns(8),\r\n      d_udf_dspms        VALUE 'X',           \"Flags to display error message\r\n      d_udf_dsn_msg(50).\r\n\r\nDATA: d_udf_lngth TYPE i,\r\n      d_udf_count TYPE i,\r\n      d_udf_start TYPE i,\r\n      d_udf_nchar,\r\n      d_udf_xchar TYPE x.<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<pre class=\"wp-block-code\"><code>*-----------------------------------------------------------------------\r\n* @form        MACRO_UDF_DOWNLOAD\r\n* @description download data from SAP\r\n* @param       &amp;1\r\n* @par-desc    filename used, if no file given then call save as file\r\n*              dialog selected\r\n* @param       &amp;2\r\n* @par-desc    download data type\r\n* @par-val     'ASC' for ascii file\r\n*              'DAT' for dat file\r\n*              see WS_DOWNLOAD documentation for filetype\r\n* @param       &amp;3\r\n* @par-desc    itab to download\r\n*-----------------------------------------------------------------------\r\nDEFINE macro_udf_download.\r\n  IF &amp;1 EQ space.\r\n    PERFORM f_udf_get_filename USING 'S' &amp;1.\r\n  ENDIF.\r\n  IF &amp;1 NE space.\r\n    CALL FUNCTION 'GUI_DOWNLOAD'\r\n     EXPORTING\r\n*       bin_filesize                  = d_udf_fsize\r\n       filename                      = &amp;1\r\n       filetype                      = &amp;2\r\n       write_field_separator         = 'X'\r\n       dat_mode                      = 'X'\r\n     TABLES\r\n       data_tab                      = &amp;3\r\n     EXCEPTIONS\r\n       file_write_error              = 1\r\n       no_batch                      = 2\r\n       gui_refuse_filetransfer       = 3\r\n       invalid_type                  = 4\r\n       no_authority                  = 5\r\n       unknown_error                 = 6\r\n       header_not_allowed            = 7\r\n       separator_not_allowed         = 8\r\n       filesize_not_allowed          = 9\r\n       header_too_long               = 10\r\n       dp_error_create               = 11\r\n       dp_error_send                 = 12\r\n       dp_error_write                = 13\r\n       unknown_dp_error              = 14\r\n       access_denied                 = 15\r\n       dp_out_of_memory              = 16\r\n       disk_full                     = 17\r\n       dp_timeout                    = 18\r\n       file_not_found                = 19\r\n       dataprovider_exception        = 20\r\n       control_flush_error           = 21\r\n       OTHERS                        = 22.\r\n    d_udf_subrc = sy-subrc.\r\n    IF d_udf_subrc NE 0 AND d_udf_dspms NE space.\r\n      CASE sy-subrc.\r\n        WHEN  1. MESSAGE i000(zamd) WITH 'File write error !'.\r\n        WHEN  2. MESSAGE i000(zamd) WITH 'No Batch !'.\r\n        WHEN  3. MESSAGE i000(zamd) WITH 'GUI refuse file transfer !'.\r\n        WHEN  4. MESSAGE i000(zamd) WITH 'Invalid type !'.\r\n        WHEN  5. MESSAGE i000(zamd) WITH 'No authority !'.\r\n        WHEN  6. MESSAGE i000(zamd) WITH 'Unknown error !'.\r\n        WHEN  7. MESSAGE i000(zamd) WITH 'Header not allowed !'.\r\n        WHEN  8. MESSAGE i000(zamd) WITH 'Separator not allowed !'.\r\n        WHEN  9. MESSAGE i000(zamd) WITH 'File size not allowed !'.\r\n        WHEN 10. MESSAGE i000(zamd) WITH 'Header too long !'.\r\n        WHEN 11. MESSAGE i000(zamd) WITH 'DP error created !'.\r\n        WHEN 12. MESSAGE i000(zamd) WITH 'DP error send !'.\r\n        WHEN 13. MESSAGE i000(zamd) WITH 'DP error write !'.\r\n        WHEN 14. MESSAGE i000(zamd) WITH 'Unknown DP error !'.\r\n        WHEN 15. MESSAGE i000(zamd) WITH 'Access denied !'.\r\n        WHEN 16. MESSAGE i000(zamd) WITH 'DP out of memory !'.\r\n        WHEN 17. MESSAGE i000(zamd) WITH 'Disk full !'.\r\n        WHEN 18. MESSAGE i000(zamd) WITH 'DP time out !'.\r\n        WHEN 19. MESSAGE i000(zamd) WITH 'File not found !'.\r\n        WHEN 20. MESSAGE i000(zamd) WITH 'Data provider exception !'.\r\n        WHEN 21. MESSAGE i000(zamd) WITH 'Control flush error !'.\r\n        WHEN 22. MESSAGE i000(zamd) WITH 'Other unknown error !'.\r\n      ENDCASE.\r\n    ENDIF.\r\n  ENDIF.\r\nEND-OF-DEFINITION.\n\n*-----------------------------------------------------------------------\r\n* @form        F_UDF_GET_FILENAME\r\n* @description query to window system and show file dialog to choose\r\n*              a file\r\n* @param       FU_TYPE\r\n* @par-desc    type for dialog file\r\n* @par-val     'O' for 'open' file dialog\r\n*              'S' for 'save as' file dialog\r\n* @param       FC_FNAME\r\n* @par-desc    filename selected\r\n*-----------------------------------------------------------------------\r\nFORM f_udf_get_filename USING fu_type\r\n                        CHANGING fc_fname.\r\n  DATA: ld_filename  LIKE rlgrap-filename,\r\n        ld_title(20),\r\n        ld_length    TYPE i,\r\n        ld_path(80).\r\n  FIELD-SYMBOLS: &lt;lf_symbol>.\r\n  ld_path = fc_fname.\r\n  IF ld_path NE space.                 \"I-BAK010299\r\n\r\n    ld_length = strlen( ld_path ) - 1.\r\n    ASSIGN ld_path+ld_length(1) TO &lt;lf_symbol>.\r\n    IF &lt;lf_symbol> EQ '\/' OR &lt;lf_symbol> EQ '\\'.\r\n      ld_path = ld_path(ld_length).\r\n    ENDIF.\r\n  ENDIF.\r\n\r\n  IF fu_type EQ 'O'.\r\n    ld_title = 'Open file'.\r\n  ELSEIF fu_type EQ 'F'.\r\n    ld_title = 'Select file'.\r\n  ELSE.\r\n    ld_title = 'Save as'.\r\n  ENDIF.\r\n\r\n  DATA: directory TYPE string,\r\n        filetable TYPE filetable,\r\n        line      TYPE LINE OF filetable,\r\n        rc        TYPE i,\r\n        ld_window TYPE string.\r\n  CALL METHOD cl_gui_frontend_services=>get_temp_directory\r\n    CHANGING\r\n      temp_dir = directory.\r\n\r\n* Build Filter for Fileselektor]\r\n  ld_window = ld_title.\r\n  CALL METHOD cl_gui_frontend_services=>file_open_dialog\r\n    EXPORTING\r\n      window_title      = ld_window\r\n      initial_directory = directory\r\n*     file_filter       = '*.xml'\r\n      multiselection    = ' '\r\n    CHANGING\r\n      file_table        = filetable\r\n      rc                = rc.\r\n  IF rc = 1.\r\n    READ TABLE filetable INDEX 1 INTO line.\r\n    ld_filename = line-filename.\r\n  ENDIF.\r\n\r\n  d_udf_subrc = sy-subrc.\r\n  CHECK d_udf_subrc = 0.\r\n  fc_fname = ld_filename.\r\n  CHECK d_udf_chkfl IS INITIAL.\r\n  PERFORM f_udf_get_path_name USING fc_fname.\r\n  CONCATENATE d_udf_drive ':' d_udf_drcty INTO fc_fname.\r\nENDFORM.                    \"f_udf_get_filename\n\n\r\n*$*$--------------------------------------------------------------------\r\n*$*$ Lib internal use Subroutines\r\n*$*$--------------------------------------------------------------------\r\n*&amp;---------------------------------------------------------------------*\r\n*&amp;      Form  F_UDF_GET_PATH_NAME\r\n*&amp;---------------------------------------------------------------------*\r\nFORM f_udf_get_path_name USING fu_fname.\r\n  DATA: ld_char,\r\n        ld_length    TYPE i,\r\n        ld_position  TYPE i,\r\n        ld_count     TYPE i,\r\n        ld_directory LIKE pcfile-path.\r\n\r\n  ld_directory = fu_fname.\r\n  CALL FUNCTION 'PC_SPLIT_COMPLETE_FILENAME'\r\n    EXPORTING\r\n      complete_filename = ld_directory\r\n*     CHECK_DOS_FORMAT  =\r\n    IMPORTING\r\n      drive             = d_udf_drive\r\n      extension         = d_udf_extns\r\n      name              = d_udf_filnm\r\n      name_with_ext     = d_udf_fname\r\n      path              = d_udf_drcty\r\n    EXCEPTIONS\r\n      invalid_drive     = 1\r\n      invalid_extension = 2\r\n      invalid_name      = 3\r\n      invalid_path      = 4\r\n      OTHERS            = 5.\r\n  d_udf_subrc = sy-subrc.\r\nENDFORM.                               \" F_UDF_GET_PATH_NAME<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<p>Thank you. I hope this post give us more inspiration and not only copy paste. Regards. \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MACRO_UDF_DOWNLOAD Sometimes we need to download our internal table to local computer. I got this code from old code by Fredy\u00a0Santoso. All credits to him. All we need to call this macro is 1 line like this Declaration variables: Thank you. I hope this post give us more inspiration and not only copy paste. Regards&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/posts\/106"}],"collection":[{"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/comments?post=106"}],"version-history":[{"count":1,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/posts\/106\/revisions"}],"predecessor-version":[{"id":107,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/posts\/106\/revisions\/107"}],"wp:attachment":[{"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/media?parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/categories?post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sapabap.dwimansolution.com\/index.php\/wp-json\/wp\/v2\/tags?post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}