Skip to Content

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 http://www.microsoft.com/office/xml/default.mspx

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 msdn.microsoft.com . 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:

  progid="Word.Document"

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
  3
  0
  2006-09-12T15:09:00Z
  2006-09-12T16:06:00Z
  1
  48
  307
  AOK Systems GmbH
  2
  1
  354
  11.6113

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.

To report this post you need to login first.

9 Comments

You must be Logged on to comment or reply to a post.

  1. Achim Bangert
    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?

    Cheers
    Achim

    (0) 
    1. Tobias Trapp Post author
      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?

      (0) 
  2. Anonymous
    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
    Wolfgang

    (0) 
    1. Tobias Trapp Post author
      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 🙂

      Regards,
      Tobias

      (0) 
      1. Community User
        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.

        (0) 
        1. Tobias Trapp Post author
          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…

          Regards,
          Tobias

          (0) 
  3. Ogert Bogert

    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=”http://schemas.microsoft.com/office/word/2003/wordml” <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  

    (0) 
      1. Ogert Bogert
        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.

        Thanks

        (0) 

Leave a Reply