Skip to Content

How to create WORD template (add SAP OLE Link to document)?

Hello everyone!


After quite deep searching I've reached a wall and cannot move any further :(

I know coding procedure on how to initialize DOI. Then how to add mapping for links in template, load template in RAW data and open the document with changed values. But..

Problem: How to prepare such WORD document template? Document is quite complicated and I have it already localy but without variables (SAP OLE Links, attached screen from MS WORD from SAP Demo template). So the best option would be to input in specific places localy and then upload template and save it as ITAB to RAW in SAP DB table.

SAP demo template: here

If someone would seek for procedure on how to use RAW template, change variables in it and open doc here it is (shorten version):

FORM doi_initialize  
   USING    p_cust_cont_name
   CHANGING po_control   TYPE REF TO i_oi_container_control
            po_document  TYPE REF TO i_oi_document_proxy
            po_link_server   TYPE REF TO i_oi_link_server
           po_gui_cust_container TYPE REF TO cl_gui_custom_container.

DATA: l_retcode TYPE soi_ret_string.

    CREATE OBJECT po_gui_cust_container
      EXPORTING
        container_name   = p_cust_cont_name.

    CALL METHOD c_oi_container_control_creator=>get_container_control
      IMPORTING
        control = po_control
        retcode = l_retcode.

    CALL METHOD po_control->init_control
      EXPORTING
        r3_application_name = 'File export'(010)
        inplace_enabled     = 'X'
        parent              = po_gui_cust_container
      IMPORTING
        retcode             = l_retcode.

  CALL METHOD po_control->get_document_proxy
    EXPORTING
      document_type   = soi_doctype_word_document "'Word.Document'
      document_format = soi_docformat_compound "'OLE'
    IMPORTING
      document_proxy  = po_document
      retcode         = l_retcode.

  CALL METHOD po_control->get_link_server
    IMPORTING
      link_server = po_link_server
      retcode     = l_retcode.
 
 CALL METHOD po_link_server->start_link_server [...]
ENDFORM.       " DOI_INITIALIZE


FORM export_values2word 
   USING  p_blart
          po_link_server TYPE REF TO i_oi_link_server
          ps_flds_value TYPE zfm_res_add_data_mswordole_trg.

  DATA: l_retcode TYPE soi_ret_string.
**************variable 1 in WORD

  CALL METHOD po_link_server->add_string_item
  EXPORTING
    item_name  = 'Z_var1'
    item_value = lv_char "some char values
  IMPORTING
    retcode    = l_retcode

ENDFORM.           " WRITE_TEXT_TO_DB

FORM doi_open_document
   USING  ps_all_data_hdr TYPE zfm_res_add_data_msword2_src
          po_document TYPE REF TO i_oi_document_proxy.

  DATA: l_docsize TYPE i.
  DATA: ls_raw TYPE raw255.
  DATA: lt_raw TYPE STANDARD TABLE OF raw255.
  DATA: ls_doc_tmpl TYPE zfm_add_doc_tmpl.
  DATA: lt_doc_tmpl TYPE STANDARD TABLE OF zfm_add_doc_tmpl.
  DATA: l_template_versi(2) TYPE c.
  DATA: l_retcode TYPE soi_ret_string.

"[....] read RAW of template from DB to lt_raw and l_docsize

  CALL METHOD po_document->open_document_from_table
    EXPORTING
      document_size    = l_docsize
      document_table   = lt_raw
      no_flush         = ' '
      open_inplace     = 'X'
    IMPORTING
       retcode          = l_retcode.
ENDFORM. 
beztytułu.png (10.8 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Dec 14, 2017 at 06:25 PM

    So, you're missing how to use the methods of the I_OI_WORD_PROCESSOR_DOCUMENT interface. If you place "placeholder" texts (PH1, PH2, PH3, or whatever) everywhere needed in your Word Template document, then you may simply use the REPLACE method (documentation in the SAP Library). Here is the code you are missing + REPLACE + retrieve the final document :

    DATA interface_available TYPE i.
    CALL METHOD po_document->has_WORDPROCESSOR_interface
          IMPORTING
            is_available = interface_available.
    IF NOT interface_available IS INITIAL.
      DATA po_worddoc TYPE REF TO I_OI_WORD_PROCESSOR_DOCUMENT.
      CALL METHOD po_document->get_WORDPROCESSOR_interface
            IMPORTING
              wp_interface = po_worddoc.
      po_worddoc->replace( search_string = 'PH1' replace_string = 'what you want' pos = 'global' ).
      ...
      po_document->save_document_to_table( CHANGING document_size = docsize document_table = doctable ).
    
    Add comment
    10|10000 characters needed characters exceeded

    • As I said, the "flash" can be avoided. I think that the Link Server is optional in your case: if you want to store a value in the clipboard, you may use cl_gui_frontend_services=>clipboard_export (and paste_clipboard of the document proxy interface). I'm very surprised by the issue with the linefeed, I hope I can do some tests if I have time.

      For other solutions: you may use DOCX with only CL_ABAP_ZIP. A DOCX is only a ZIP with a special structure of files. One of them contains the text and you may replace placeholders with your own text. Or you may use the old format XML2003.

  • Dec 13, 2017 at 10:11 PM

    With transaction OAOR, you may load an Office document (or any file) in BDS. You may programmatically read a BDS file by using the class CL_BDS_DOCUMENT_SET, and then you know how to load it in DOI.

    Instead of using DOI, don't you want to implement a document in DOCX format with custom XML parts. It has already been discussed in SCN (class CL_DOCX_DOCUMENT), and on the Web (custom XML parts in Microsoft Office). It may be a little bit longer as you already did some work on DOI, but it's a more recent technology (and it's faster to generate, and it works in background).

    Add comment
    10|10000 characters needed characters exceeded

    • Many thanks for your reply!

      I am aware of uploading template using OAOR. The problem is that I don't know how to prepare doc before uploading, so how to add variables connected to SAP OLE Link as shown on attached screenshot.

      The idea of using XML might be of course the best option, I've found this conception during research however on client's server there is no such class nor the whole OpenXML class :(