未做测试, 暂记备查.
原文地址: http://blog.sina.com.cn/s/blog_3eefd36c01008cjc.html
说明:
将数据文件和word模板放在c盘根目录下, fill_ms_word_with_data 这个form里实现了邮件合并(不使用宏).
REPORT z_mailmerge_test3.
INCLUDE ole2incl.
DATA: txt_work_file LIKE rlgrap-filename VALUE
'C:/data.txt'.
DATA: doc_work_file LIKE rlgrap-filename VALUE
'C:/MailMerge.doc'.
DATA: h_word TYPE ole2_object,
wordapp TYPE ole2_object,
worddoc TYPE ole2_object,
wordobj TYPE ole2_object.
START-OF-SELECTION.
PERFORM start_ms_word.
PERFORM fill_ms_word_with_data.
*&---------------------------------------------------------------------*
*& Form start_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'.
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. " start_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