1.创建模板
选中要设为变量的文本,点击格式文本,然后在属性页签设置变量名
2. 代码生成
参考ZABAPDOCX包下示例程序,可直接执行下载word文档
DATA: lv_wordapp TYPE ole2_object,
lv_worddoc TYPE ole2_object,
lv_wordadoc TYPE ole2_object,
lv_wordcont TYPE c LENGTH 8,
lv_path TYPE string VALUE 'C:\Users\X\Desktop\DOXC.docx',
lv_path1 TYPE string VALUE 'C:\Users\X\Desktop\DOXC.pdf'.
CREATE OBJECT lv_wordapp 'WORD.APPLICATION'.
PERFORM errorhandler.
SET PROPERTY OF lv_wordapp 'Visible' = 0.
CALL METHOD OF lv_wordapp 'Documents' = lv_worddoc.
PERFORM errorhandler.
CALL METHOD OF lv_worddoc 'Open'
EXPORTING
#1 = lv_path.
PERFORM errorhandler.
CALL METHOD OF lv_wordapp 'ActiveDocument' = lv_wordadoc.
PERFORM errorhandler.
CALL METHOD OF lv_wordadoc 'Content' = lv_wordcont.
PERFORM errorhandler.
CALL METHOD OF lv_wordadoc 'SaveAs'
EXPORTING
#1 = lv_path1
#2 = 17.
PERFORM errorhandler.
CALL METHOD OF lv_wordapp 'Quit'.
FREE OBJECT lv_wordapp .
FORM errorhandler.
IF sy-subrc <> 0.
WRITE: / 'OLE- Error:', sy-subrc.
CALL METHOD OF lv_wordapp 'Quit'.
FREE OBJECT lv_wordapp.
STOP.
ENDIF.
ENDFORM.
3. 适用场景
简单文件的输出,对于下载后不需要数据处理的文件来说,更优于excel文件的生成
附ZABAPDOCX包地址:
GGitHub - AntonSikidin/zcl_docx: Best way to create Microsoft Word docx from abap