Skip to Content

Last month, i published my abstraction class to manage Word OLE link. It can generate complete (and complex) word document, but it is a little slow for big tables.

OLE is an old technology… DOCX is an XML file extension… Enough to change my mind about word file generation. Exit OLE, welcome XML 🙂

I updated my abstraction class to generate DOCX file from ABAP directly, without any OLE usage. I know there are actually some projects that want to do that (abap2docx for example). But i think theses projects are too complex to use, or not yet usable in the true life.

With my class, it never be easier to generate DOCX. You never have to use or understand XML.

Here is the code of the “hello word” program.

docx_generation-sap-abap-hello-world[1].jpg

The class is simple, but can manage complex documents !

Here is the feature list :

  • Empty document creation or with use of template (docx, dotx, docm, dotm)
  • Write text with or without style (character style and/or paragraph style)
  • Option to manualy apply bold, underline, italic, strike, subscript, superscript, small caps, font name & size, font color, highlight color, letter spacing
  • Management of alignment, indent, spacing before/after paragraph
  • Break line, page, section, continuous section
  • Write table with or without style (and option to define cell format : bold, color…)
  • Write Header / footer
  • Write end note / foot note
  • Write comments
  • Write numbered label (figure, table…)
  • Write table of labels (figures, tables…)
  • Choose portrait/landscape, manage page border
  • Add images
  • Add canvas
  • Insert table of content (toc)
  • Add and manage document properties
  • Create and insert custom fields
  • Style creation (character/paragraph)
  • Manage files in SAP Web Repository for template/image (SAPWR, access with transaction SMW0)

In the download file, you will find a test program that contain the class CL_WORD and a demo of how to use it. You will find also some images and 1 template. Theses files are used by the test program, but are not necessary for the class itself.

French presentation can be found here : SAP : Générer un document Word DOCX en ABAP

And there is a direct download link (remember that you will need SAPLINK to install .slnk file) : http://quelquepart.biz/telechargements&file=L2RhdGEvbWVkaWFzL1pDTF9XT1JEX0RPQ1guemlwKjU1NGMxNQ&source=SCN-DOCX

A use case of the class can be found in ZAUTODOC : Automatic generation of technical BW documentation

Last but not the least, source code is visible here : Show code in Nugget – Quelquepart

Feel free to comment here 🙂


My others blogpost :

LISTCUBE replacement : a new way to display data

ZAL11 : a replacement for AL11

ZTOAD – Open SQL editor

Abstraction class to generate MSWORD with SAP using OLE

ZAUTODOC : Automatic generation of technical BW documentation

To report this post you need to login first.

58 Comments

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

  1. Rainer Hübenthal

    Hi,

    sounds great.

    ABAP2XLSX is fine you don’t know anything about XML 🙂 Unfortunetly ABAP2DOCX seems to be in hibernation…

    As all your other projects are really great I will give it a try for sure!

    (0) 
        1. Sébastien HERMANN Post author

          Hello,

          Have you chaanged the path of image/template to appropriates ones on your computer ?

          I think the generated word file dont use the template 😉

          If everything is ok in the demo program and there is the error, could you please expend the word error popup and write me the exact error message please ? And also send me the generated word file ?

          (0) 
        2. Sébastien HERMANN Post author

          About the separate class:

          For a customer development, i always use separated class (SE24).

          But for tools i share with other people, i find it more simple to use an include SE38. Easier to read, only 1 object to transfer…

          You could easily recreate a class if you require it, but which usage require SE24 class instead of embedded one ? (just remove “demo” code and you could use the class as an “include” in any program)

          In next release i will separate demo code and class code.

          (0) 
  2. Mansoor Ahmed

    Many thanks for ZTOAD. Simply an awesome tool to use.

    Quick question! I have downloaded this utility but it has a file ZTEST_DOC.slnk. How to load it in SAP?

    Thanks,

    (0) 
    1. Sébastien HERMANN Post author

      Thank you Ahmed, i like it too 😀

      For .slnk, just use saplink, it is same as .nugg but with only 1 object inside (a little smaller and a little faster for me to generate)

      (0) 
  3. Paul Hardy

    This is fantastic.

    Do you see this as a possible replacement for SMARTFORMS/SAPSCRIPT in some cases? You would create a word template with your logo and the general format and then use SAP to dynamically fill the business data?

    Cheersy Cheers

    Paul

    PS ABAP2XLS is used in real life at my company, but I have wrapped around it several helper classes to make it easy for our developers to use.

    (0) 
    1. Sébastien HERMANN Post author

      Hello,

      My remark about real life usage concern only DOCX generation 🙂

      If abap2docx was as wonderfull as abap2xls, my tool never be born 😉

      regarding your question about smartform/sapscript replacement. If the user requirement is to have DOCX file, yes i think you can replace smartforms. Create the word template with all header/footer, and all styles you need. You could also insert all image you will need.

      Remember that actual class drop existing document content, so you have to write all pages in program. But if required, it is very easy to change the class to add an option to keep actual text content of the template. (edit : Done for the next release)

      For example, if you have only individual fields to fill, you can create a complete docx into word, replace all variable part with word custom fields. And with the class you just have to define custom fields value 🙂

      (0) 
      1. Cesar Fermin

        I have a Customer requirement like this.. I need Show a Word templete with  header, logo, footer and more thing like a Smartforms with an ABAP program…

         

        (0) 
    2. Shai Sinai

      Hi Paul,

      May you please elaborate on your helper class? (What exactly does it do?)

      Wouldn’t it be useful to enrich the original project with it?

      (0) 
      1. Paul Hardy

        When the ABAP2XLSX project was on the SCN code exchange I found it really easy to contribute, and I was right up there on the leader board of active contributors.

        I have to say I am struggling with the environment where the project is hosted now, as I think other people are – because I think the project has totally stalled since it moved. I could be looking in the wrong place but there does not seem to have been any activity for months.

        In any event I have a helper class where I store the code that does not change between different ALV reports. I call a method of the helper to change the ALV object into an ABAP2XLSX object, then the program at hand makes application specific changes, and then another method of the helper sends the modified EXCEL object as an email.

        There is more detail on this in chapter 11 of my good old book. Moreover, if you got to the SAP Press Website and look up my book you can download the associated materials for free, and their you will find class ZCL_EXCEL_EMAILER and some of it’s friends, which are my helper classes. Looking at the code I just noticed it still has a BREAK HARDYP in it, oh well, too late now.

        The code has lots of comments but they say WHY things are done, not how, as the ABAP2XLS methods are so well named it is obvious what they do i.e. you don’t have to think too hard what a method call like WORKSHEET->FREEZE_PANES does.

        I asked my editor why SAP Press let people download supplementary material for their books for free, and she said having some sort of secret code you get with each book was just too much extra effort for no real benefit. In fact when people download such things they are often so intrigued by the content (code) they end up buying the book anyway.

        Cheersy Cheers

        Paul

        (0) 
        1. Shai Sinai

          Hi,

          First, I would like to thank you for the book and sharing the code.

          Actually I do own the book (the massive hard-copy version), but I’m ashamed to tell I didn’t have a chance to read it carefully yet.

          However, from the community point-of-view, I think that it would be useful to include these helper classes in the project itself.

          Why don’t you feel comfortable with gitHub?

          There is even a Git client for ABAP project – abapGit, which should make the life easier, but I haven’t tried it myself yet.

          (0) 
        2. Rainer Hübenthal

          Paul Hardy wrote:

          I have to say I am struggling with the environment where the project is hosted now, as I think other people are – because I think the project has totally stalled since it moved. I could be looking in the wrong place but there does not seem to have been any activity for months.

          Paul

          Unfortunatly that true. I expanded ABAP2XLSX to have more options to graphics. Unfortunetly the conversation was hard with Ivan and replies took weeks. So finally I gave up to recontribute my extensions back to everyone

          (0) 
  4. Paul Hardy

    Hello again,

    I have been talking to my boss about this, and he says it would be lovely if we could generate a word document out of SAP using ABAP and then email it as a PDF so the target could not change it.

    If I was doing a similar thing in ABAP2XLS then I would programmatically make the worksheets all password protected, with a 32 digit randomly generated GUID. That would set it in concrete for all time.

    In word there is also a setting to make the document read only, so that would be the way forward.

    Do you think there is a way to generate the word document so that it becomes a PDF, or is setting the read-only flag the solution?

    Cheersy Cheers

    Paul

    (0) 
    1. Sébastien HERMANN Post author

      Hello,

      With word, you could lock the template in “read only” with password protection to unlock.

      Then simply use the locked template with my class: all generated documents will be locked in read only with the same password to unlock.

      Note that it is not a strong protection, but do you search a military level lock ? 😉

      Or you could probably transform DOCX to PDF with external program, but you also have to protect your PDF because unprotected PDF could be modified easily 🙂

      (0) 
  5. Helena Fortun

    Hello Sébastien,

    I´m very interested in your development.

    How can I have a look into the abap code of your abstraction class if I don´t have SAPLINK?

    Many thanks in advance!

    Helena

    (0) 
    1. Sébastien HERMANN Post author

      To have a look, you can simply open it in a text editor. Special char is escaped, but i think you can “read” the code.

      But to import in sap, i’ve only created the saplink version. What’s your problem with saplink ? 🙂

      EDIT : For next release i will try to add a simple text version as the program contain only abap code (no text, no screen…)

      (0) 
  6. Achim Pellen

    Hi Sébastien,

    sorry but I cannot open the generated DOC file in MS WORD 2013.

    It says that WORD cannot open the file because some parts are missing or invalid.

    Could you check that please?

    Thx a lot

    Achim

    (0) 
    1. Sébastien HERMANN Post author

      Hello,

      Have you changed the path of image/template to appropriates ones on your computer ?

      I think the generated word file dont use the template 😉

      If everything is ok in the demo program and there is the error, could you please expend the word error popup and write me the exact error message please ? And also send me the generated word file ?

      (0) 
  7. Paul Hardy

    I have been playing with your Word generation class and – as I said before – it is fantastic, you are to be congratulated, especially for sharing it with the world.

    In this brave “new” OO world SAP do not like us having INCLUDE files, so I am going to generate a Z class out of the local class.

    As always I am then going to mess around with it like nobodies business.

    However, as with ABAP2XLS no doubt when I start to use this there will be some new business requirements to overcome which will involve expanding the functionality.

    Since I do have a handle on XML, just like I waffled on about in my book, it is not the end of the world to add extra things, as I demonstrated using ABAP2XLS.

    If I end up doing the same thing here, I will update this blog to let the SCN world know what the new requirement was, and how I solved it.

    Cheersy Cheers

    Paul

    (0) 
    1. Sébastien HERMANN Post author

      thanks a lot Paul.

      About external class, i’m not closed but i’ve no idea on how to add documentation. I know that i can create doc object on the class, but it will not be exported with saplink, exept if i add another plugin. I’m tiedous with adding lot of plugins on saplink…

      Actually, with the include class, all documentation is easily included (and you can see there is a lot of documentation in my class 😉 ). It’s also easy to read to find the appropriate class

      If someone have a clean solution, i could change my mind and package an external class…

      About the specific requirements, before thinking of code, i suggest you to always think about word class. You can do a lot of things with class (embed in your template).

      The 2 known missing functions are : bullets and footnote.

      Footnote is easy to add and will probably be in next release.

      bullets are not so hard to add, but will be a pain to use. In the xml, for each bullet you have to firstly declare a new bullet group with a lot of parameters…

      So, easy to do for a dev who need, but hard to reuse by someone else. That why i’ve not added for the moment.

      i will be happy to read your enhancements 🙂

      (0) 
      1. Florian Henninger

        hi Sébastian,

        I do not know what kind of release you are running. Maybe you can use ABAP-Doc, that would end in no extra-plugin or anything else.

        Drumroll.. and it will generate a lovely documentation for all those “SE24”-Users with a proper release 😆

        Have a look here: ABAP Doc

        ~Florian

        (0) 
    1. Sébastien HERMANN Post author

      My project home is on my blog, but in french 😯

      About github… not very nice for sap objects, except if you use the abapgit client which is in “alpha” release. So, not for the moment 😉

      I didnt know the link you gave us. Could i edit to add my others projects ?

      ZAL11, ZTOAD are used by lot of people, ZLISTCUBE a little less ^^

      (0) 
      1. Uwe Fetzer

        Yes, you can edit and add your other projects (that’s the purpose of the document).

        Regarding Github: have a look at the other Github projects. We are using Github also for project management and ticket handling. If you want others to contribute to your project you simply need such functions.

        (0) 
        1. Sébastien HERMANN Post author

          The main purpose of my post is to share my code, not to search beta tester or debugger, even if i will always happy if someone find a bug, enhance my code or ask a feature 😉

          Actually when people find a bug or enhance or have feature request, they simply reply to the blog post.

          I tried github for a php project. It take me lot of time and the feedback was not greater than when i use simple blog post, but im sure git is a better solution for big project with many contributors

          (0) 
  8. Sébastien HERMANN Post author

    Hello,

    I just released v1.1.

    Class is now in a separate “include”.

    Demo program has been renamed ZDEMO_DOCX.

    A TXT file is included in the zip package with CL_WORD in plain text

    Changelog :

    • Add option to keep loaded template content
    • Add get_zip_file method
    • Add write_note method
    • Add write_comment method
    • Add image insertion in table
    • Add xml insertion in table for complex cell content
    • Add option on write_text to insert label number
    • Add option on write_line to get XML instead of buffer into doc
    • Add option to save zip file on server
    • Add options on method write_toc. Now you can write a toc of specific label(for ex.: figure)
    • Fix bug when template path is invalid
    • Fix dump when reuse an image
    (0) 
  9. Paul Hardy

    Mr.Sebastien,

    If it is all right with you, I would like to briefly mention your invention in my next book, in the same way I mentioned ABAP2XLSX and Ivan Femia in the last one.

    I would like to send you an email to discuss this. If you were to “follow” me on SCN (I have just “followed” you) then I can send you a direct message via SCN.

    In addition the ZCL_WORD INCLUDE is just an INCLUDE containing one local class, as I understand it. Is there a reason this not just a Z class, that would achieve the exact same thing, would it not?

    These days the “official” position at SAP is not to use (re-usable) INCLUDES at all,. There is no blanket rule that fits all situations, but I have found that 99% of the time I can replace a re-usable INCLUDE with a Z class of some sort.

    To go into a more detailed example – I had an INCLUDE which contained about a dozen FORM routines all to do with ALV. I changed that into a Z class with all the FORM routines as methods of that class.

    At first glance that seems pointless, what did I achieve by doing that apart from doing the “right” thing, an argument I consider specious at best.

    This way though I get rid of the problem whereby I remove the risk of adding something into the INCLUDE which might cause a syntax error in one of the many programs that contain that INCLUDE. I could add a FORM routine to the INCLUDE with the same name as the one of the programs that uses it.

    Cheersy Cheers

    Paul

    (0) 
    1. Sébastien HERMANN Post author

      Hi Paul,

      Just clicked on “follow” 🙂

      About Z class : as previously discussed in this post, i’m open. There are many reason why i share an include :

      – easier to share (1 object contain all the stuff)

      – less footprint (after saplink import, the “activation” list is smaller, easy for you to check what have been imported, to avoid problem…)

      – easier for documentation (all method docs are embbed in the include, no need another saplink plugin)

      The main reason is for documentation. i’m tiedous to require a saplink plugin to install the tool…

      (0) 
  10. David Joseph

    Hi Sébastien,

    The way you have implemented this functionality is really neat and indeed commendable.

    I understand that the methodology you have followed is what MSDN specifies as well to manipulate OOXML documents. ( https://msdn.microsoft.com/en-us/library/aa982683(v=office.12).aspx )

    Never thought anyone could have implemented it so smoothly in ABAP. Neat.

    I was checking this on one of our systems and find that the Paragraph styles does not get set to the text fragment. (The style in question is “Title” which is present and not complicated to refer to as well… right?).

    the  “word/styles.xml” that is generated does not have any style information when we create the form using  cl_docx_form=>create_form( ) in your constructor.

    Most probably this would be an issue with our landscape. Ever faced this issue before?

    I have not tried with templates yet.

    Thanks and Regards,

    David

    (0) 
    1. Sébastien HERMANN Post author

      Hi David,

      Thank you for your feedback 🙂

      No issue on your landscape. When you create a document “from scratch” there is no style embbed. If you want 1 style, you have to define it before.

      This is the first reason why I added the “create from template” function 😉

      You could simply open word, save an empty docx, and use it as a Template for program (even if it’s better to create a proper dotx Template, it will work with the docx)

      (0) 
  11. Wolfgang Propfe

    Sounds great, I’ll give it a try. Is it free to use at a company or are there any restrictions for productive commercial use? I may suggest using your class at work, and this will be the first question they ask…

    Thanks,

    Wolfgang

    (0) 
    1. Sébastien HERMANN Post author

      Hello It is the same for all code i share here or on my blog :

      – You can install and use

      – You can modify and rename. If you think your modifications can be usefull to others, feel free to share your modifications here

      – You can sell my code to customers. I just ask you to not lie to your customer about the owner of the code (dont try to say it’s your code).

      – As the program does not contain any malicious code, you are responsible of all damages done by the code. Before use, you can check and review the source to be sure 😉

      (0) 
      1. Wolfgang Propfe

        Thank you for the fast response. I will certainly give due credit, and I’m excited to try it later today… Three cheers to your project and to ABAP2XLSX (by Ivan). Both are very useful. Users love Office documents.

        Wolfgang

        (0) 
      2. Wolfgang Propfe

        I gave it a try and found several incompatibilities with ECC6. I was able to copy and adjust missing parts to the point where I got a clean compile. I can generate text, but the graphics (or something else) seem to cause a corrupt document. Still looking into that. If you connect, we can talk about details and a few suggestions, then you can choose what to incorporate in your solution.

        Hope to talk to you soon.

        Wolfgang

        (0) 
        1. Sébastien HERMANN Post author

          Hi Wolfgang,

          I’m sorry that you found incompatibilities with your ECC6 version. Since SAP call all releases “ECC6” (you have to dig deeper to find the true version), it’s not easy to explain which ECC6 was tested and which one wasnt…

          So, i will be happy to include your fixes if it dont break compatibility with other version 😉

          Regarding your doc corruption, maybe you use word 2007 ?

          There is a fix not yet published here to correct the corruption on word 2007.

          In method write_table, search paragraph which start by
          * Define table width

          In this paragraph, replace :

              ELSE.
                CONCATENATE lw_xml
                            '<w:tblW w:w="'
                            lw_tblwidth
                            '"/>'
                            '</w:tblPr>'
                            INTO lw_xml.
          

          By

              ELSE.
                CONCATENATE lw_xml
                            '<w:tblW w:w="'
                            lw_tblwidth
                            '" w:type="dxa"/>'
                            '</w:tblPr>'
                            INTO lw_xml.
          

          Not easy to write abap here with IE… hope it will be ok…

          (0) 
    1. Roberto Vacca

      Hi,

      very nice work!! 🙂

      I’ve to tell you that this instruction

      CALL METHOD o_doc->write_table EXPORTING content = lt_cle tblwidth = 9300.

      in demo program, makes the docx file unreadable.

      To bypass, I’ve only avoided “tblwidth” definition.

       

      Anyway I would like to ask you what do you think to use a class like the one you implemented to create a Label Print mechanism?

      Is there a real enhancement instead of using OLE technology?

      Thanks for your work!

       

      Regards!

       

      (0) 
  12. Hamza Chioua

    Hello,

    Please, i’m trying to build a word doc via ur class zcl_word, and I would like to know if there is way to delete something i’ve already writed into the .doc before i save it ??

    any idea ?

    (0) 
  13. Anurag Abbi

    The links do not work !

     

    Your link redirects to http://quelquepart.biz/static3/show-code&id=0046&scn

    I get this message:

    500

    There is an error while processing this request.

    (0) 

Leave a Reply