SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Get SAP data and mail mail merge it to a MS word document



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> OLE2, Excel, WinWord
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1636

PostPosted: Fri Feb 27, 2009 3:53 pm    Post subject: Get SAP data and mail mail merge it to a MS word document Reply with quote

Code:
************************************************************************
*   Program name:  ZZJF_WORD_46UP                                      *
*   Author      :  HR Expert author                                    *
*   Date        :  April 2005                                          *
*   Description :  This demo program will work for version 4.6 onward. *
*                  It will get SAP data and mail merge it to a MS word *
*                  document.                                           *
*======================================================================*
*            M O D I F I C A T I O N   H I S T O R Y                   *
*======================================================================*
*
************************************************************************
report zzjf_word_46up no standard page heading
                 message-id zh
                 line-count 65
                 line-size 132.

tables: pernr.

infotypes: 0000, 0001, 0002.

*----------------------------------------------------------------------*
* parameters and selection options.
*----------------------------------------------------------------------*
selection-screen begin of block blk0 with frame title text-l02.
parameters: p_year(2) type n default '15'.
selection-screen end of block blk0.


type-pools: abap.

data: begin of empinfo occurs 0,
        first_name(30) type c,
        last_name(30) type c,
        years(2) type c,
      end of empinfo.

data: ee_selected(1) type c.

********************* OLE Defines **********************************
include ole2incl.

data: h_word  type ole2_object,
      wordapp type ole2_object,
      worddoc type ole2_object,
      wordobj type ole2_object.

data: txt_work_file like rlgrap-filename value
         'C:\Program Files\SAP\SAP_DATA_FILE.txt'.

data: doc_work_file like rlgrap-filename value
         'C:\Program Files\SAP\MS_WORD_DOC.doc'.


data: begin of fieldnames occurs 5,
        field(60) type c,
      end of fieldnames.
********************* end OLE Defines *****************************


*----------------------------------------------------------------------*
initialization.
*----------------------------------------------------------------------*
  perform init_parameters.

*----------------------------------------------------------------------*
start-of-selection.
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
get pernr.
*----------------------------------------------------------------------*
  clear: empinfo, ee_selected.
  perform gather_infotype_data.
  check ee_selected ne space.
  append empinfo.

*----------------------------------------------------------------------*
end-of-selection.
*----------------------------------------------------------------------*
  if not empinfo[] is initial.
    perform mail_merge_data_to_ms_word.
  else.
    write: / 'NO DATA AVAILABLE FOR THIS SELECTION!'.
  endif.

*----------------------------------------------------------------------*
top-of-page.
*----------------------------------------------------------------------*
  perform print_report_header.


*&---------------------------------------------------------------------*
*&      Form  GATHER_INFOTYPE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form gather_infotype_data.

  data: begin of phifi occurs 5.
          include structure phifi.
  data: end of phifi.

  data: hire_date type d.
  data: fire_date type d.
  data: cutoff_date type d.
  data: years(2) type n.

  call function 'RP_HIRE_FIRE'
    exporting
      beg       = '18000101'
      end       = '99991231'
    importing
      hire_date = hire_date
      fire_date = fire_date
    tables
      pp0000    = p0000          "input
      pp0001    = p0001          "input
      pphifi    = phifi.         "output

  check fire_date eq '99991231'.
  cutoff_date = sy-datum.

  check not hire_date is initial.
  check hire_date < cutoff_date.

  call function 'COMPUTE_YEARS_BETWEEN_DATES'
    exporting
      first_date          = hire_date
      second_date         = cutoff_date
    importing
      years_between_dates = years.

  check years >= p_year.

  write years to empinfo-years no-zero.

  rp-provide-from-last p0002 space pn-begda pn-endda.
  if pnp-sw-found eq '1'.
    empinfo-last_name = p0002-nachn.
    empinfo-first_name = p0002-vorna.
  endif.

  ee_selected = 'X'.
endform.                               " GATHER_INFOTYPE_DATA
*&---------------------------------------------------------------------*
*&      Form  INIT_PARAMETERS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form init_parameters.
  pnptimed = 'D'.                      "set to today radio button

endform.                               " INIT_PARAMETERS
*&---------------------------------------------------------------------*
*&      Form  PRINT_REPORT_HEADER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form print_report_header.

endform.                               " PRINT_REPORT_HEADER
*&---------------------------------------------------------------------*
*&      Form  MAIL_MERGE_DATA_TO_MS_WORD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form mail_merge_data_to_ms_word .

  perform clear_existing_data_file.
  perform download_employee_data_to_pc.
  perform start_ms_word.
  perform fill_ms_word_with_data.

endform.                    " MAIL_MERGE_DATA_TO_MS_WORD
*&---------------------------------------------------------------------*
*&      Form  CLEAR_EXISTING_DATA_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form clear_existing_data_file .
  data: file_name type string.
  data: result type abap_bool.
  data: rc type i.

  file_name = txt_work_file.

  call method cl_gui_frontend_services=>file_exist
    exporting
      file            = file_name
    receiving
      result          = result
    exceptions
      cntl_error      = 1
      error_no_gui    = 2
      wrong_parameter = 3
      others          = 4.

  call method cl_gui_cfw=>flush.
  if result eq abap_true.
    call method cl_gui_frontend_services=>file_delete
      exporting
        filename = file_name
      changing
        rc       = rc
      exceptions
        others   = 0.

* flush to execute the deletion
    call method cl_gui_cfw=>flush.
  endif.

endform.                    " CLEAR_EXISTING_DATA_FILE


*&---------------------------------------------------------------------*
*&      Form  DOWNLAOD_EMPLOYEE_DATA_TO_PC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form download_employee_data_to_pc.
  data: file_name type string.
  data: cnt type i.

  data: lt_download type standard table of string,
        lv_download type string,
        lv_hstr(1024) type c.

  class cl_abap_char_utilities definition load.
  constants: lc_tab type string
                    value cl_abap_char_utilities=>horizontal_tab.

  field-symbols: <field> type any,
                 <data_tab_wa> type any.

  perform get_all_field_names.

  clear lv_download.
  loop at fieldnames.
    write fieldnames to lv_hstr left-justified.
    if sy-tabix > 1.
      concatenate lv_download lc_tab lv_hstr
                  into lv_download.
    else.
      concatenate lv_download lv_hstr
                  into lv_download.
    endif.
  endloop.
  append lv_download to lt_download.

* then the data table
  loop at empinfo
       assigning <data_tab_wa>.
    cnt = 0.
    clear lv_download.
    sy-subrc = 0.
    while sy-subrc = 0.
      cnt = cnt + 1.
      assign component cnt of structure <data_tab_wa> to <field>.
      check sy-subrc = 0.
      write <field> to lv_hstr left-justified.
      if cnt > 1.
        concatenate lv_download lc_tab lv_hstr
                    into lv_download.
      else.
        concatenate lv_download lv_hstr
                    into lv_download.
      endif.
    endwhile.
    append lv_download to lt_download.
  endloop.

  file_name = txt_work_file.
  call function 'GUI_DOWNLOAD'
    exporting
*     BIN_FILESIZE                  =
      filename                      = file_name
*     FILETYPE                      = 'ASC'
*     APPEND                        = ' '
      write_field_separator         = 'X'
*     HEADER                        = '00'
      trunc_trailing_blanks         = 'X'
*      col_select                    =
*      col_select_mask               =
*   IMPORTING
*     FILELENGTH                    =
    tables
      data_tab                      = lt_download
   exceptions
     file_write_error              = 1
     no_batch                      = 2
     gui_refuse_filetransfer       = 3
     invalid_type                  = 4
     no_authority                  = 5
     unknown_error                 = 6
     header_not_allowed            = 7
     separator_not_allowed         = 8
     filesize_not_allowed          = 9
     header_too_long               = 10
     dp_error_create               = 11
     dp_error_send                 = 12
     dp_error_write                = 13
     unknown_dp_error              = 14
     access_denied                 = 15
     dp_out_of_memory              = 16
     disk_full                     = 17
     dp_timeout                    = 18
     file_not_found                = 19
     dataprovider_exception        = 20
     control_flush_error           = 21
     others                        = 22.
  if sy-subrc <> 0.
    case sy-subrc.
      when 1.
        raise file_write_error.
      when 2.
        raise no_batch.
      when 3.
        raise gui_refuse_filetransfer.
      when 4.
        raise invalid_type .
      when 5.
        raise no_authority.
      when 6.
        raise unknown_error.
      when 7.
        raise header_not_allowed.
      when 8.
        raise separator_not_allowed.
      when 9.
        raise filesize_not_allowed.
      when 10.
        raise header_too_long.
      when 11.
        raise dp_error_create.
      when 12.
        raise dp_error_send.
      when 13.
        raise dp_error_write.
      when 14.
        raise unknown_dp_error.
      when 15.
        raise access_denied.
      when 16.
        raise dp_out_of_memory.
      when 17.
        raise disk_full.
      when 18.
        raise dp_timeout.
      when 19.
        raise file_not_found.
      when 20.
        raise dataprovider_exception.
      when 21.
        raise control_flush_error.
      when others.
        raise unknown_error.
    endcase.
  endif.

endform.                               " DOWNLAOD_EMPLOYEE_DATA_TO_PC
*&---------------------------------------------------------------------*
*&      Form  GET_ALL_FIELD_NAMES
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_all_field_names.
  type-pools: sydes.
  type-pools slis.

  data:  td type sydes_desc.
  data: name_header type sydes_nameinfo.
  data: name like name_header.
  data: types_header type sydes_typeinfo.
  data: type like types_header.
  data: prev_tabix like sy-tabix.
  data: continue_field(1) type c.
  data: search_str(40) type c.
  data: s_length(5) type p.

  data: begin of stru_tab occurs 0,
          name(30) type c,
          fldname(30) type c,
          index    like sy-tabix,
          length(3) type n,
          type(1)   type c,
          decimals(2) type n,
        end of stru_tab.

  clear: td.
  describe field empinfo into td.

  clear: stru_tab[], stru_tab.
  do 1000 times.
    read table td-types into types_header index sy-index.
    if sy-subrc ne 0.
      exit.
    endif.
    type = types_header.
    stru_tab-index = type-idx_name.
    stru_tab-type  = type-type.
    stru_tab-length = type-length.
    stru_tab-decimals = type-decimals.
    check stru_tab-index ne 0.
    append stru_tab.
  enddo.

  do 1000 times.
    read table td-names into name_header index sy-index.
    if sy-subrc ne 0.
      exit.
    endif.
    name = name_header.
    if continue_field ne space.
      read table stru_tab index prev_tabix.
      if sy-subrc eq 0.
        concatenate stru_tab-fldname name-name into stru_tab-fldname.
        modify stru_tab index prev_tabix.
        clear: prev_tabix, continue_field.
      endif.
    else.
      loop at stru_tab where index eq sy-index.
        stru_tab-fldname = name-name.
        prev_tabix = sy-tabix.
        modify stru_tab.
      endloop.
    endif.
    if name-continue eq '*'.
      continue_field = 'X'.
    else.
      continue_field = space.
    endif.
  enddo.
* now build fieldnames
  loop at stru_tab.
    fieldnames-field   = stru_tab-fldname.
    append fieldnames.
  endloop.
endform.                               " GET_ALL_FIELD_NAMES
*&---------------------------------------------------------------------*
*&      Form  OPEN_MS_WORD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form start_ms_word .
  data: xcode   like sy-xcode,
        ok_code like sy-xcode,
        rc type i,
        platform type i,
        registry_entry(255) type c,
        reg_value type string,
        temp_dir type string.
  data: version_str(80) type c,
        word_version type i.

  data: documents type ole2_object.

* Start with OLE automation
  call method cl_gui_frontend_services=>registry_get_value
    exporting
      root                = cl_gui_frontend_services=>hkey_classes_root
      key                 = 'Word.Basic\CurVer'
      value               = ''
    importing
      reg_value           = reg_value
    exceptions
      get_regvalue_failed = 1
      cntl_error          = 2
      error_no_gui        = 3
      others              = 4.
  if sy-subrc <> 0.
    raise download_problem.
  endif.
  call method cl_gui_cfw=>flush.
  version_str = reg_value.

  shift version_str left by 11 places.
  move version_str to word_version.

  if word_version < 8.
    create object wordobj 'Word.Basic'.
  else.
    create object worddoc 'Word.Document'.
    get property of worddoc 'Application' = wordapp.
    set property of wordapp 'Visible' = 1.
    get property of wordapp 'WordBasic' = wordobj.
    call method of wordobj 'FileClose'.
  endif.
  call method of wordobj 'AppShow'.

* Serienbriefdatei Жffnen
  if word_version > 8.
    get property of wordapp 'Documents' = documents.

    call method of documents 'Open'
      exporting
        #01 = doc_work_file  "file name
        #02 = 0.              "confirm conversion
*        #03 = 1              "ReadOnly
*        #04 = 1              "AddToRecentFile
*        #05 = ''             "PasswordDocument
*        #06 = ''             "PasswordTemplat
*        #07 = 0              "Revert
*        #08 = ''             "WritePasswordDocume
*        #09 = ''.            "WritePasswordTemplate
*        #10 = 4.             "Format 4=plain text
*                        #11 = 1255.   "Encoding: e.g. hebrew=1255
  else.
    call method of wordobj 'FileOpen'
      exporting
        #01 = doc_work_file
        #02 = 0.
  endif.

endform.                    " OPEN_MS_WORD
*&---------------------------------------------------------------------*
*&      Form  FILL_MS_WORD_WITH_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form fill_ms_word_with_data .
  data: passwort(15).

  call method of wordobj 'MailMergeMainDocumentType'
    exporting
      #01 = 0.
  call method of wordobj 'MailMergeOpenDataSource'
    exporting
      #01 = txt_work_file
      #02 = 0
      #03 = 1
      #04 = 0
      #05 = 0
      #06 = passwort.
  call method of wordobj 'MailMergeEditMainDocument' .
  call method of wordobj 'MailMergeToDoc'.

endform.                    " FILL_MS_WORD_WITH_DATA
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> OLE2, Excel, WinWord All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.