Skip to Content
Author's profile photo Jerry Wang

Manipulate Docx document with ABAP

There is a useful class CL_DOCX_DOCUMENT provided by SAP which could support read and write access to a word document with file extension “.docx”.

This document gives a brief introduction about its usage and could be used as a starting point to build your own application which needs to manipulate word document via ABAP.

Office OpenXML

Starting with Microsoft Office2007, when you create a new word document, you will get a file with “.docx” file extension by default which follows the Office openXML format. You can find its detailed definition from wiki.

For example, I create a very simple word document which contains a header area, a paragraph with three lines as body, and a picture.

/wp-content/uploads/2014/05/clipboard1_461558.png

According to Office OpenXML protocal, after you change the file extension from “.docx” to “.zip”, its icon changes to an archive file and thus could be opened via winrar. All information about my sample document are spreaded inside a series of xml files in the archive file ( plus media file like picture, music and video if the word document has such one).

The most efficient way to study is create a word document by yourself, change extension to zip and explore it.

/wp-content/uploads/2014/05/clipboard2_461559.png

Using CL_DOCX_DOCUMENT to read word document

I use the following sample code to explain how to use this class.

In order to avoid unnecessary local variable declaration, I use the new feature “inline declaration” available in release 740. If this version is not available for you, just replace them with old manual declaration for local variable.

  DATA: lv_content  TYPE xstring,
        lo_document TYPE REF TO cl_docx_document.
  PERFORM get_doc_binary USING 'C:\Users\i042416\Desktop\test.docx' CHANGING lv_content.
  lo_document = cl_docx_document=>load_document( lv_content ).
  CHECK lo_document IS NOT INITIAL.
  DATA(lo_core_part) = lo_document->get_corepropertiespart( ).
  DATA(lv_core_data) = lo_core_part->get_data( ).
  DATA(lo_main_part) = lo_document->get_maindocumentpart( ).
  DATA(lo_image_parts) = lo_main_part->get_imageparts( ).
  DATA(lv_image_count) = lo_image_parts->get_count( ).
  DO lv_image_count TIMES.
    DATA(lo_image_part) = lo_image_parts->get_part( sy-index - 1 ).
    DATA(lv_image_data) = lo_image_part->get_data( ).
  ENDDO.
  DATA(lo_header_parts) = lo_main_part->get_headerparts( ).
  DATA(lv_header_count) = lo_header_parts->get_count( ).
DO lv_header_count TIMES.
DATA(lo_header_part) = lo_header_parts->get_part( sy-index - 1 ).
DATA(lv_header_data) = lo_header_part->get_data( ).
ENDDO.

Comments

1. you can get a instance of word document via method cl_docx_document=>load_document. It is necessary to pass the document binary data with type xstring into this method. I don’t list source code of subroutine get_doc_binary as it is not relevant. Just find it from attachment.

2. The system administrative data like author, creation and last modification date are stored in so called “Core property part”, which could be fetched via document instance got in step1. Once you own the instance of Core property part, you can get its binary data via method get_data().

The returned data has xml format( so does all the left other kinds of parts in this document ) so it could be easily parsed via DOM or SAX parser.

/wp-content/uploads/2014/05/clipboard3_461560.png

3. from document instance we can get main part instance. Its binary data includes all the three body line texts with their font color:

/wp-content/uploads/2014/05/clipboard4_461561.png

/wp-content/uploads/2014/05/clipboard5_461562.png

4. The binary data of all pictures embedded in the word document could be retrieved via two steps. Firstly get the image part collection from main part instance and then loop each image part instance from the image collection. The get_part method accepts the index starting from 0. The way to read header block information is exactly the same.

Using CL_DOCX_DOCUMENT to change word document

See the nice document How to – Add Custom XML Parts to Microsoft Word using ABAP from Leon Limson.

You could also achieve the same requirement with the respective class below.

/wp-content/uploads/2014/05/clipboard6_461564.png

Further reading

If you would like to know how a word template is merged with data from xml file ( for example a response file from web service ), you can find technical detail in my blog Understand how the word template is merged with xml data stream.

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo abilash n
      abilash n

      Thanks Jerry for amazing share with nice explanation. Let me try it out.

      Author's profile photo Otavio May
      Otavio May

      Hello.

      I'm trying to create a new document based on an existing document, but did not figure it out how to made it.

      I want to create a copy (from a document that's already exists), change some body lines and save this new document.

      I didn't found some method to do this. Can you help me?

      Best regards,

      Otavio May

      Author's profile photo Ebrahim Hatem
      Ebrahim Hatem

      Hallo together,

      to upload the doc, you need to use this mehtod.

       


      SPAN {
      font-family: "Courier New";
      font-size: 10pt;
      color: #000000;
      background: #FFFFFF;
      }
      .L0S52 {
      color: #0000FF;
      }
      .L0S55 {
      color: #800080;
      }
      DATAg_data_part1 TYPE xstring,
      g_data_part2 TYPE xstring.

       


      SPAN {
      font-family: "Courier New";
      font-size: 10pt;
      color: #000000;
      background: #FFFFFF;
      }
      .L0S33 {
      color: #4DA619;
      }
      .L0S52 {
      color: #0000FF;
      }
      .L0S55 {
      color: #800080;
      }
      .L0S70 {
      color: #808080;
      }
          CALL METHOD cl_docx_form_mix_ft_util=>load_file
      EXPORTING
      im_file_name g_filename2
      im_local     'X'
      RECEIVING
      re_data      g_data_part2.

      Best regards

       

       

      Author's profile photo Xiaoguang Li
      Xiaoguang Li

      学习学习,不明觉厉!