一个程序:SAP调用WORD,不使用宏

未做测试, 暂记备查.

 

原文地址:  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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值