Abstraction class to generate MSWORD with SAP using OLE
I wrote an abstraction class to generate complex MS Word document from SAP. This class is delivered “ready to use” and cover, i hope, a large panel of usage.
Why this class ?
OLE syntax is not easy and find help is a pain. I learned all i need to know and, to never have to do it again, i have written this class.
Now to write text into word, i simply use the method “write_text” or “write_table” to write a complete table 😉
What you can actually do with the class :
- Create document from scratch or with a template (dotx)
- Write text with or without style (font style or paragraph style)
- Use option bold, italic, underline, choose font, font size, font color
- Break line, Break page, Section, section continuous
- Write table, with or without Table style, and option to define format by cell (bold, background color…)
- Write simple header/footer
- Choose orientation landscape/portrait
- Add image
- Add canvas
- Insert table of content
- Insert custom field
- Change the title of the word window
- Save document and close word
As OLE is an old method, and seem to be a little slow for big document (in particular if you have a lot of table). So this class could be used to help all people that have a problem with OLE syntax, it contain almost all solutions 😉
In the download, you will find a test program that contain the class CL_WORD and a sample of how to use it. You will find also some image 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 : Faire communiquer SAP et MS WORD grâce à OLE – Quelquepart
And there is a direct download link (remember that you will need SAPLINK to install) : http://quelquepart.biz/telechargements&file=L2RhdGEvbWVkaWFzL1pDTF9XT1JEX09MRS56aXAqOGQwZmVh&source=SCN-OLE
Feel free to comment here 🙂
My others blogpost :
LISTCUBE replacement : a new way to display data
ZAL11 : a replacement for AL11
ZAUTODOC : Automatic generation of technical BW documentation
The Links do not work.
Temporary problem ? Links are working here. Could you retry ?
Hi Sebastien, great blog!
Quick question, If I already have a template with the custom fields (MergeField), is possible update it with your class?
I saw that I can create and update custom fields with the methods insert_custom_field and create_custom_field, but if I'm not wrong is only possible update custom fields that was created with your class, but if I already have the custom field, for instance the XML below, is there a way to update it?
<w:instrText xml:space="preserve"> MERGEFIELD RECNNR </w:instrText>
You could update custom field created into ms word.
The class cannot update the value hardcoded into the “displayed” document. So. You have to use the “refresh” function when you open the doc with word to see the updated value. (same for the table of content if you use one)
That’s why i personaly use a dotm template with an “auto refresh macro”
Sorry, this reply is applicable only for the new class https://blogs.sap.com/?p=134512
For OLE version you are right :
The only way to set a value for a custom field is to use create_custom_field method, which require that the field doesnt exists in the document...
method insert_custom_field allow you to insert the "anchor" in the document. But you already have one in your template, so you dont need to use it.
method update_fields is just to refresh the displayed values with stored ones.
You could try to create a method to update the value of a custom field that already exists... or try the new class which is far more faster and build docx file.