Skip to Content

Generation of Word Documents Using XML Technologies

Today’s Office program support XML formats for saving and exchanging documents. There are two major standards: OASIS Open Document Format for Office Applications and Microsoft Office Open XML which is developed for Microsoft Office 2007.

At the moment the Microsoft Office Suite supports the following XML formats:

  • DataDiagramingML for MS Visio,
  • SpreadsheetML for MS Excel,
  • PresentationML for MS Powerpoint and
  • WordprocessingML for MS Word.

This weblog is about WordprocessingML – in short WordML. Using WordML you can create Word documents from your SAP Web Application Server.

Introduction into WordML

Since Word 2003 we can save documents in XML, more precisely in WordML. I suggest you create a Word document with the text content “Hello SDN community” and save this document as an XML document. The result will look like the following:

Hello SDN community!

In fact I simplified this example such the structure is easier to recognize. The document starts with an XML processing instruction mso-application progid=”Word.Document”. The root element is w:wordDocument.

You will immediately recognize similarities to HTML. The text content of the document resides within the element w:body. A single paragraph is the content of an element w:t within an element w:p.

If you write the text “Hello SDN community!” in boldface mode this is represented by an element w:b:

Hello SDN community!

The element w:r represents a run element: the container for data in a Word document. This container can contain text, pictures and so on. In the example above we have a text content within the element w:t. The element w:rPr represents the so called run properties for the paragraph and w:b sets asian and latin characters bold. 

Word is a very powerful tool so it is not surprising that the syntax is very complex. You can download the  WordprocessingML Schemas and additional information from

Tips for Beginners

For your first XSL transformation generating WordML following tips may be useful:

Create a Word document as a template that you fill with additional elements w:p. Try to keep it as simple as possible and try to delete elements you don’t need.

Inspect the WordML representation of a Word document with the XML Toolbox. This tool is a free Word add-in that shows the current word document in an external viewer. You can download this tool from . In fact you need the .NET framework to use it.

Don’t forget the special Word processing instruction. Using XSLT you can insert it with following code:


Possible Applications

Typical applications for WordML applications are normalizations of Word documents. We can change the o:DocumentProperties element to change the author’s or company’s name:

  First Try
  Tobias Trapp
   Tobias Trapp
  AOK Systems GmbH

With XSLT you can perform many cleanup tasks like removing comments or removing any direct formatting that hasn’t been applied with a style. Many people apply such transformations because they want to be sure that the receiver (perhaps a customer) of a word document gets only the visible information but not changes, comments and so on.

A typical application in R/3 is the generation of documentation. We can write a transformations that works on data stored in your R/3 system, the RTTI information of your ABAP classes, the transportation system and so on.

I don’t suggest that you start to write your own output management system after reading this blog. In my opinion the lightweight XSF output of SAP smartforms is more appropriate for generating different output like HTML-eMails, PDF and Word documents.

The techniques described above are not restricted to Word. They are XML formats for other word processors like Open Office using OASIS Open Document Format for Office Applications.

You must be Logged on to comment or reply to a post.
  • Hi Tobias,

    thanks a lot for this blog! For those of us forced to provide documentation in Word format your blog is a boon. Maybe you can expand on your ideas for retrieving the metadata of the relevant development objects in a future blog?


    • Hi Achim,

      this is quite an interesting question.  Concerning documentation of ABAP classes I refer to VSEO*-Views.

      Longtexts are a little bit difficult because you can get them as SAPscript texts using the function module ‘DOCU_GET’. Because I don’t like SAPscript I tried to get the HTML texts but (as expected) they are not well formed XML so you can’t work on them using XSLT. So here is my first question to the experts: Can we get longtexts in XHTML?

      The second question is about longtexts of inherited methods: In 6.20 we can’t change these longtexts (but with polymorphic method of interfaces it is OK). In fact I can’t see any technical reasons for it so it’s possible that SAP decided to restrict it due to academical reasons: Liskov’s substitution principle says that inherited classes should obey the same contracts as their parent classes. So here is my second to the experts: Is this behaviour a bug or a feature?

      Who can help?

  • Hi Tobias,

    is there a way to generate a word document, then let the user change/format the word document and get the data back to SAP using your techniques?

    Thanks & regards

    • Hi Wolfgang,

      that’s a good question. At the moment two solutions come to my mind.

      1.) Let SAP generate a document that contains a macro. After the user makes his changes the document he can send the data back to SAP via RFC.

      2.) The User makes his changes. Then you collect the documents and perform a XSL transformation on it the extracts the data. This transformation can create a file (think of an XML document, batch-input map…). The file is transfered back to the SAP system.

      Solution 1. uses the same mechanisms you we know from OLE integration. If you want to know how this works I suggest to read the book “ABAP Best Practices”. The difference is that you create an XML file instead starting an WORD instance on the frontend.

      Solution 2. has a batch flavour. That can be useful in some applications – perhaps you are afraid of RFCs from WORD-documents to the R/3.

      I hope this helps. If you should have further questions and comments feel free to contact me. And if you have a working solution then write a blog about it 🙂


      • What if there was a 3rd option or 4th option? One that allowed the user to actually work with edit, change, save, share and colloborate on the document containing the data generated from R/3 and then put right back into?

        Duet during the Demo Jam Las Vegas showed something along these lines, so I would recommend checking out the Demo Jam video available here on SDN.

        The other option is something in “planning” at the moment in terms of a mashup… oh the suspense… hmmm, ok no I must wait to tell everyone.

        • Hi Craig,

          you are absolutely right – there are many more possibilities: we could even more write an ABAP framework that creates WORD documents and evaluates them after getting them back.

          I’m looking forward to your solution…


  • Hi all,<br/>I`ve made a BSP application containing a “page with flow logic” called test.htm. Within that file I`ve put the following code.<br/><br/><?mso-application progid=”Word.Document”?><br/><w:wordDocument xmlns:w=”” <br/>  w:macrosPresent=”no” w:embeddedObjPresent=”no” w:ocxPresent=”no”  <br/>  xml:space=”preserve”><br/>  <w:fonts><br/>    <w:defaultFonts w:ascii=”Times New Roman” w:fareast=”Times New Roman” <br/>      w:h-ansi=”Times New Roman” w:cs=”Times New Roman”/><br/>  </w:fonts><br/>  <w:body><br/>    <w:p><br/>      <w:r><br/>        <w:t>Hello SDN community!</w:t><br/>      </w:r><br/>    </w:p><br/>  </w:body><br/></w:wordDocument><br/><br/>When I execute it IE opens and shows the content above. I want MS Word to open. I would have appreciated any kind of help regarding this problem. Hopefully it`s just a minor change of code that makes it work.<br/><br/>I`ve tried to rename the file to fileextension .doc, .docx and .xml, with the same result.<br/><br/>Thanx <br/><br/>Ogert  

      • Thanx Raja,
        Now IE tries to open MS Word, though it can`t find the file it tries to open. From the “File download window” that appears, one can see that the file has been given the name ‘test_htm’ and that it is a Microsoft Word document of size 513 bytes. When I push open the IE shows “The page cannot be displayed”.

        Is there any more changes that has to be made?
        Sorry to bother you.