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.
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.
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.
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.
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.
3. from document instance we can get main part instance. Its binary data includes all the three body line texts with their font color:
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.
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.