Skip to Content
Technical Articles
Author's profile photo Sébastien HERMANN

Generate DOCX file in ABAP

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.

And there is a direct download link (remember that you will need SAPLINK to install .slnk file) :

Download link : https://www.mediafire.com/folder/if3ljcnesme7y/ABAP

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

 

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

Assigned Tags

      86 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Rainer Hübenthal
      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!

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Thank you 🙂

      Hope you could find it usefull

      Author's profile photo Rainer Hübenthal
      Rainer Hübenthal

      Will there be a separate class? I guess this would be more reuseable....

      And sad to say that, but the generated docs file cant be opened with Word 2010

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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 ?

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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.

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello,

      You can take the new 1.1 version where the class is in a separate include file.

      Author's profile photo Former Member
      Former Member

      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,

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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)

      Author's profile photo Former Member
      Former Member

      I just did. But it says

      Start import of nugget

      /wp-content/uploads/2015/11/cl_word_832428.png

      Nothing below that. Please find attached.

      Author's profile photo Former Member
      Former Member

      Another thing. When I choose Display Nugget. It says Empty Nugget.

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      yes, its not a nugget but a slinkee file. Dont you have a second tab for slinkee files in your version of saplink ?

      Author's profile photo Former Member
      Former Member

      Oops! My bad. Truly an honest mistake. I just ignored that tab.

      Author's profile photo Former Member
      Former Member

      Done.

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      it take many years before I use it 😉

      Author's profile photo Silvio Miranda
      Silvio Miranda

      Another great piece of code.. thank you!!!

      Author's profile photo Paul Hardy
      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.

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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 🙂

      Author's profile photo Cesar Fermin
      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...

       

      Author's profile photo Former Member
      Former Member

      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?

      Author's profile photo Paul Hardy
      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

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo Rainer Hübenthal
      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

      Author's profile photo Andrea Borgia
      Andrea Borgia

      There's a new batch of contributors and we're trying to be more active.

      You're welcome to come by at GH.

      Author's profile photo Paul Hardy
      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

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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 🙂

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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...)

      Author's profile photo Former Member
      Former Member

      Hello,

      I don´t work now in the IT and don´t have authorisation.

      Thanks!

      Helena

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello, you can take new version 1.1 which contain a txt file with class content

      Author's profile photo Achim Pellen
      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

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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 ?

      Author's profile photo Achim Pellen
      Achim Pellen

      Hi Sébastien,

      you're right it's because of the missing images/template.

      It's working now.

      Thx a lot!

      Achim

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      good ! thank you for the feedback 🙂

      Author's profile photo Paul Hardy
      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

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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 🙂

      Author's profile photo Florian Henninger
      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

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello Florian,

      dont it work only on eclipse ?

      Author's profile photo Florian Henninger
      Florian Henninger

      Hi Sébastien,

      have to check on that. I get back to you.

      ~Florian

      Author's profile photo Uwe Fetzer
      Uwe Fetzer

      Looks really promissing. Would be great if you can create a Github project (with a separate class).

      I've added this page to ABAP Open Source projects (feel free to modify the document as soon as you have a real project home 😉 )

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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 ^^

      Author's profile photo Uwe Fetzer
      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.

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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
      Author's profile photo Paul Hardy
      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

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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...

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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)

      Author's profile photo Former Member
      Former Member

      That makes sense and it works. Thanks Sébastien

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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 😉

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog 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...

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      It is now possible to look at the source code without downloading the zip ! (same for all programs i deliver, check on my site)

      Show code in Nugget – Quelquepart

      Author's profile photo Roberto Vacca
      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!

       

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello Roberto,

       

      Thanks for your feedback 🙂

      Which version of word do you use ? I didnt found any problem with the tblwidth option.

       

      OLE technology is very usefull (and very fun to show the document created without any action... a kind of magic)

      BUT, it's very very very slow !

      If your requirement does not involve a lot of OLE commands, you can use it. In other cases, if will take too much time to process.

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Wow, sorry, didn't realized that cl_word version here is so old !

       

      I will release a new version that fix this issue that occurs only on word 2007

       

      Author's profile photo Hamza Chioua
      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 ?

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello,

       

      There is no way actually to delete a fragment you already added in the doc.

       

      Instead of write something that you will maybe have to delete later in the code, maybe you could write it later ?

      Or if not possible, you could use method "insert_virtual_field" to add a temporary anchor in your document.

      Later, you can replace this anchor by any kind of text, by using the method "replace_virtual_field". If you want to delete the anchor (and dont want to write something),, simply replace the anchor by space.

       

       

      Author's profile photo Anurag Abbi
      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.

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      It is working now. Sorry for this temp issue

       

      Author's profile photo Sergey Pochuhaylov
      Sergey Pochuhaylov

      Hello,

      is it possible to insert the standard barcode (tcode se73) into docx like in smartforms/adobe interactive forms?

       

      regards, Sergey

       

       

      Author's profile photo Sergey Pochuhaylov
      Sergey Pochuhaylov

      Hello,

      is it possible to insert the standard barcode (tcode se73) into docx like in smartforms/adobe interactive forms?

      Regards, Sergey

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello,

      Honestly I dont know, never used this in ECC.

      If you can find an abap way to generate an image for your barcode, you can add this image in the document. If you cant, maybe Word manage barcode directly, but I dont know how...

       

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Version 1.4 released. This new version :

      • Fix the dump problem logged here in comment
      • Add Forms (rectangle, elipse, rounded corner rectangle)
      • Insert attachement
      • Various bug fix
      • Various enhancement to build xml fragment and inject it in other part. For example, build a complex paragraph and inject it in a table cell, or build a table and inject it in a table cell
      Author's profile photo Oliver Hemeyer
      Oliver Hemeyer

      Hi Sébastien,

      great work!

      Is is possible to open a created document inplace?

      Like in Demo-Programm SAPRDEMO_WORD_NOTEPAD.

      Kind regards!

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello,

      Never asked before !

      It could be easilly done. You could create a method which open embeded word (but you will have to save the doc), or do this in your specific abap part after the docx generation.

       

      Author's profile photo Eva Priester
      Eva Priester

      Great work, Sébastien!
      We are using your class and it's working fine!

      But now, we have a problem with Windows 10. 🙁

      We are generating a Word Document from a Word Template (using your class). If we are doing this on a Windows 10 and trying to open this generated Word Document on Windows 10, we are getting an error message and it's not possible to open this Document.

      Error Message:
      "Xml parsing error

      Position: Component /word/document.xml, Line: 0, Column: 0"

      If we open this Word Document on Windows 7, it is working. Afterwards we can also open it on Windows 10.

      Has anyone ever had this issue?

      Best regards!

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello Eva,

      The windows version may not be the rootcause of the issue.

      Have you the same Word version on both pc ?

      Which version of the class do you use ?

      An Issue specific to word 2007 was solved in v1.3

      Author's profile photo Eva Priester
      Eva Priester

      Hi Sébastien,

      thanks for your quick reply (and sorry for my late response).

      On the Windows 7 PC, we are working with Word 2010. On the Windows 10 PC, there is Windows 2016 installed.

      We are using v1.3 of your class.

       

      What I found out in the meantime is, that the document.xml-file is not readable.

      I generated a Word Document using your class. Afterwards I renamed it (added “.zip” to the Filename). Then I tried to open the File /word/document.xml with a XML Editor, but that is not possible.

      But when I open the Word Document and save it and then again open the /word/document.xml-File, then I can open it with a XML Editor.

      So I think something is wrong there. (But I have no idea, what is wrong.)

       

      Best regards,

      Eva

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello,

      It’s a very strange behavior. xml file may be openable…

      Could you send me your broken file by e-mail please ? (if not confidential content of course)

      I will have a look to find the issue.

       

      Author's profile photo Eva Priester
      Eva Priester

      Many thanks to Sebastien for the helpful email conversation!

      The error, I described, comes up because of an additional function we were using in our report (and has nothing to do with Sebastiens Class).

      Nevertheless, I found the error and corrected our program.

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Great news, thanks Eva ?

       

      Author's profile photo Felipe dos Santos Mattos
      Felipe dos Santos Mattos

      Hi Sébastien,

      I'm trying attach a xml file into docx file using insert_attachment method but doesn't works. I tryed change the insert_attachment method, including the schema to .xml attachment. Doesn't worked. Can you help me how i can insert the .xml attachment into a .docx file using class ZCL_WORD?

      The image from file is showed in the WORD file, but doesn't open.

       

      Method INSERT_ATTACHMENT.

      CASE lw_extension.

      WHEN 'xml' OR 'bin'.
      lw_ct_string 'application/vnd.openxmlformats-officedocument.oleObject'.
      lw_rel_string 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject'.

       

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello Felipe,

      Unfortunately, it's not so easy.

      For all kind of files i tested (except the ones already managed : doc, ppt, xls), Word use a specific mecanism to include them.

      Your xml file have to be packaged (by the class) in a .bin file (wich compression format ?) and renamed ([1]Ole10Native without extension part) with 3 others files :

      The 3 descriptions files are not text readable, so, i dont know how to generate them.

      I'm very interested if you find how to manage theses kind of attachement

      Author's profile photo Willian Szrajer Gruber
      Willian Szrajer Gruber

      Hello Sébastien,

      I'm creating a table but can't figure out how to change the column widths, is it possible using your class?

      Also, congratulations on this excellent development

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      hello,

      method write_table have a parameter tblwidth.

      Full width table have a very big value, i dont remember how much exactly, maybe around 7000 or 9000.

       

      Author's profile photo Marius Cocis
      Marius Cocis

      Hello Sébastien,

       

      and thank you for the god-send program you created ?

      It enables me to add an image in the header and it works so fast, but is there a way to display the Word app after creating the document?

      So I don’t need automatic saving of the document, only displaying Word app and leave it at User’s decision if he wants to save or not. 

       

      Thank you.

      Best regards from Cluj, Romania,

      Marius Cocis

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello,

      This class is to generate a docx file (or docx file content). You cannot leave it in the memory to open it directly into ms word. You need to save as a temp file and the ask ms word to open this file (using a system command).

       

      But, if your need is to start to generate a file without save, maybe you could use OLE.

      Check my abstraction class. Most of the method have same name/usage between the 2 class, so you could switch without big rework.

      https://blogs.sap.com/2015/10/30/abstraction-class-to-generate-msword-with-sap-using-ole/

      Author's profile photo Marius Cocis
      Marius Cocis

      Hello again,

      and thank you for replying to my previous comment so quick. I went down the XML way and I have one more question. I need to write the address of some partner with a border.

      _____________________

      | line address 1                 |

      | line address 2                 |

      |____________________|

      So the address has multiple lines. I tried doing this with a table, but I get border for each cell (so also a border under line address 1. I don’t want that. I could write all text in a table with just 1 cell, but haven’t been able to find any escape character for ‘new line’.

       

      Help is greatly appreciated.

      Thank you. Have a nice day, Marius

       

      later edit:

      I solved this problem with a bit of extra coding:

      I%20removed%20the%20inner%20borders

      I removed the inner borders

      Author's profile photo Marius Cocis
      Marius Cocis

      Hello Sébastien,

       

      I am strugling for 2 days to insert an image in the header. The reason for this is that the image may change in time and I also need different images for different languages, so insert the image in template won’t help me.

      Inside the write_headerfooter method I have called the insert_image method, asking for VIRTUAL parameter but the problem is that the image is not displayed, instead it looks like “The picture can’t be displayed”. Can you please have a look at the word (inside the zip ofcourse) and let me know what could be the problem? And maybe provide some hints on how to adjust the coding.

      My guess is that somehow the relations are not created ok, that’s why he can’t display the picture. But the image is in the zip file and the path to it in the relationships document.

      https://1drv.ms/u/s!Au-VzpoQZGGzibNhPk2BGr-MA6seug?e=SIWHSu – I uploaded the file there

      Thank you.

      Have a great day,

      Marius Cocis

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello Marius,

      I’ve a good news and a bad news.

      – I’ve found why technically your image is not displayed in the header and i could help you to solve your issue.

      – It’s not managed by the class for the moment.

      You are right about pointing the relation.Header images are not managed in the main relation file. Word create a specific relation file for each header/footer which require special relation.

      But the class actualy add all relations only in the main relation file (document.xml.rel).In your case, you need the image relation added inside a new header3.xml.rels file.

      As a quick and dirty fix, i duplicated document.xml.rel to header3.xml.rel file. And your image is displayed.I think you could easily write a little method in the class to do that.

      A proper solution may be to delete all unnecessary lines in header3 relation file, or better, to scan all headers/footers before saving and to generate required header/footer relation files.

      If you are not confident with theses solutions, maybe you simply could create a template with 1 image in the header, and change the image file with the class (i think you have to create a method to do that).

      Or, without any class modification, create a template with all headers variants, and use the header you want with the class.

      Tell me which solution you finaly choose ?

      Kind regards,

      Sebastien

      Author's profile photo Cesarina Vanessa Lappot Guzman
      Cesarina Vanessa Lappot Guzman

      Hello Marius:

       

      Would you mind sharing your solution regarding the image on the header? I can't get it done. 🙁

      Author's profile photo Philip Peacock
      Philip Peacock

      I've picked up a lot of things from SAP Community over the years, but your work in these areas is GREAT.

      I have one question regarding Virtual Fields.  I am wanting to use a template to build predefined forms so users can make their own and just have "tags" that can be replaced.  (For example,  "[SALES_ORDER_HERE]" to be replaced by the actual data once retrieved.

       

      Can you insert a virtual field and save a document without replacing it?

      Can we then substitute at a later time?

       

      Even if you don't answer, the code is awesome!

      Author's profile photo Sébastien HERMANN
      Sébastien HERMANN
      Blog Post Author

      Hello,

      You cant use virtual fields to do that as virtual field are temporary placeholders which cannot survive outside the ZCL_word class.

       

      But, you can use ms word custom fields to do that. Prepare your template with many "custom fields" into ms word and insert them when you want.

      Then you can use your template with zcl_word and set the value of each custom fields.

      The only problem is that zcl_word is currently not be able to refresh the display of custom fields inside the generated document.

      So you will have to ask to ms word to refresh the fields manualy when you open the doc, or you can embed a macro (in the template) which do it automatically on doc opening (that's what i did to refresh the TOC).

       

      Author's profile photo Lukas Sanders
      Lukas Sanders

      Hello Sebastién,

      thank you for your awesome work!

      I was facing an issue though when taking the XSTRING generated from the method GET_DOCX_FILE, converting it to SOLIX_TAB and attaching it to an e-mail (using CL_BCS). Upon opening, a message appeared stating that the document is corrupted and cannot be opened, although it could be restored.

      It worked fine when saving the document locally with the SAVE method, so I added another method which similarly generates a SOLIX_TAB and exports it directly. This SOLIX_TAB worked fine with e-mails and the error disappeared.

      The problem was most likely caused by the fact that the system has not yet been converted to Unicode. Maybe this can be helpful to others.