Skip to Content
Technical Articles

Generate a Word File using ABAP with Static/Dynamic Data

One might have come across a requirement where a word file needed to be generated with static data or with a mix of static and dynamic data. There are actually many ways like using OLE Objects or Microsoft Word Content Control Tool Kit, etc. But the problem with these options is that either we require some additional software (which usually not possible on client system or may require some approvals) or we couldn’t get the versatility while formatting the file.

This blog will cover:

  • How we can generate a word file with image and formatting using ABAP.
  • File format RTF and its corresponding RTF Code which will be used to solve the above problem statement.

Introduction

RTF or Rich Text File was created by microsoft and as the name suggest, it’s rich in formatting, styles,etc. than the usual text file. Best thing about RTF (file extension: .rtf) is, if saved as ‘.doc’ or ‘.docx’, it will be converted to the mentioned format without any issue.

To create RTF file either we can use any word processor(not in our case) or we can use RTF Code (RTF code consists of plaintext, commands, escapes, and groups: Plaintext contains seven bit (US ASCII) characters except for \, {, and }. ) within the ABAP code when we are clubbing the data in an internal table with which the file is to be generated.

NOTE: As we are using some sort of code (RTF Code), therefore, there’s need of program/software to render it. Nothing to worry here, MS Word, OpenOffice, or any word processor software will do us this favor.

Solution

Problem Statement: Using ABAP, generate a word file in format ‘.doc’, where the doc will be having an image in center with paragraph below it with font as Helvetica – 12pt and first line of it as bold.

For this requirement, I am using static text stored in SE61 as General Text. But there’s a clause to it that, I have written the RTF code as needed in text itself, so that when it’s call in ABAP we will have everything in internal table. In my opinion, in case of dynamic text too with certain template, we can use the same method but keep some placeholder which is unique and can be replaced easily by modifying the table itself in code if placeholder exists for that line.

Let ZSCM_TEXT_MVTMSG is General Text created using Edit Documents TCODE (SE61). Below is the data:

{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0{\pard \qc
{\pict\pngblip
89504E470D0A1A0A0000000D494844520000010E0000005A0802000000C6BB2FA7000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000155D49444154785EED9B09701DD595861F90090E79DDB7B7273304061C60180203242435052105041754650666268C191617CC6420A99A9A7108018C654B32D818E4455E6463E37D41D846DE648CF77D41B6B5CBFB22CBB6BC62BC1B2FF2D2F39F7BFBF56BBDC5EE84A7648A9CAAAF5452F73DE79E7BEEF9EFBDDDEF29F27E24C630CC1561A9304C28582A0C130A960AC38482A5C230A160A9304C28582A0C130A960AC38482A5C230A160A9304C28582A0C130A960AC38482A5C230A160A9304C28582A0C130A960AC38482A5C230A160A9304C28582A0C130A960AC38482A5C230A160A9304C28582A0C130A960AC38482A5C230A160A9304C28582A0C130A960AC38482A5C230A160A9304C28582A0C130A960AC38482A5C230A160A9304C28582A0C138AAC49E5BD48AC5700FC79990669EF267950A4B6545CDE1B486AE0B7095E4FC537BF0C491E7CCF97410510B45284B1057F846D5A1310B24710F4906A9574D76F10BC9E96902DFD663EB812DE796B901DA9508857394444911CB71CA453A8EE52B3E4BB3D23B177E977DF83E46ABA885BF0E63756EDDFF3DBA478537F52772A9E780345228C2464B31E297D25A19CF8232D94EDE94A06648F6A08F18E7CAEA2D1814CE6015BFC99649ED1D6B74AA428C1157AF44183F7E263F4FE6C799742CA94DE84610A57397E4B907C3741729C7193A4660102612893AC931DA96054A3FE47B80D9ABB4B73776B9726591FC88B7E83B723B1596335773FEE46DD8D62D3F34E41FC1626F57DCD197A83BD608CEEEED3DCC600C7F555373983DA3A7D54B3386F479C451334B789FA726BB5F27F88750FDC0598C8C2570CF770D4DD197577884D2F3ABD6512DF89C4A68D9461047B51EC8FBA1F58FD34A7E8EA167D2501CFC50F987B2AA2D4FB81A83BD45617836D7CD0235CF5319C0FEFB7EACA34774FA0BB5D51779BF1691B67904D7A0BE62A60EBF413CEA03BECFA89BABB3760BB27EACE33E7DDE50CD43CAD06ADE0AAC870463D691D582953E45BEDD59BA798A551678046F59A29660532BC6A014C40D49D6D4E8FD122E2DF25DB874DF758727AD1F5076FEAEEA168A2D32098FA06E313B91EA1E588A7CCA3F5519AC1A46618DD7C63E1234EFF6BA919DCA247FC2CBCD1AE9C2E434A6AAF38A4ED7BDD1E922E93D9226B07B0DCEF594DB375B74A732B3477B95EF9682C3F7E0BE32CB8C33A55ADB96B35B75A3B5D6294E678A9C7CFE29BED6513BDA2776B34775D1CFC89F6DB81A87ECCEE7F4DA2823191738B655F95DA8545FAB21FA691CAFB2F18EE46E9618DA8E94025853060B86C8A8E18DC353ADD4217AAAF3AD9123F1BB486FFA4BE32651C9E07FDC8DA36075DEB687FAED086DB4C6587EE463E686D5C8C12917DF9DD018C143D42308B8CA53F76FAC81DC637844F2AA6C7CD2DB3651DA071D01671E2CFC6A83BD32CBBDD41EF681FB7724A3A987B5747DDADD20A2D9549BD46E9C2C09BB43363CC99B75F412D887CC96499A82AAD798698FB035C49DC25C317D3A417B339B92FA5C54B6F7036C106FC34666BB460612A273C631C419BB53A354647AA8D8A133446CF0D3727DE482D55A845EDECFA59B825DB570686A668D00F16D823324FDCD7276B52C11238EA5746B3AA800A8ABB6F7C32909A923C41C303E562DDD3745DCD6BFF5BAC5DE5324132B327461BD55D8CFA3CA33ED7A82A169417080F5635DAE6979CA2B8C3AF2315AF02D66A1797E87BFA1A355D8DFA6E46752FE3CC1A2972F455A1ADFFA53743419F0A780E29157818748F79E60B39BB0875B5BEBFD8A8C995A3EB62540ED7DDCDB2A450F48BC5AAF6B4302B3F708864963C671C4305209F0869AB7EA4D858976BD4C1B69B51DFC3D8BF48A3D1358A6D1DED41D250594DEA689CC74E8281C0ED52D1F0BE518B01E61975DD8C2DA5BABB1E3D52C95E986A94DD412669C7085A4865BA98F377295249975E486562A1940AAEAFD00F0E898F575180E0ADA172154072C677300E231848A55A3B35C6A87E8B86565B60ECFE447862DB1C6D1E64F797EDD163D12D76DD4C99CC4AEDE22CB1F16D1A94EF7CC3BBE6FC9FD9FD32CC4556C89A5440B788B3065BA4CCEFC5CF8CC5F7D2C642196C6BED5A4817C1991116A6D65B2A6276F504A10AD7AD158DFF6B95DC10CBC51624E9F25D7BD67F1BE729EF72CEE6790ED15116A452A59D9B2266DE94F396EC0B9D4EEE6A5051A27D8D7EB8C81E2C6728E85301CF61A4822BEFFE8DD5B44A167A957661B1D8F0A2352C1AEB1A1F5D67DBAE2814EE2659BBEBB4F363AC8937385853608B7EFB3F689E44F0153AA4726196D8946B8D8BD218317CC5F09F9B2BDF300E7D2E9A7E4DF9A4EE22B1010F492B4AA67EA6D4A87ACAC6C1354F76879FBDFEDEDA3442D0028C1E3769A77BDAFDFF2AFD18C1D7954AA576698E58FA13A7737CBC3EAAC71652D9A8ED7C3AF6BABCDB0DA3F8A9B50BC71335179B8C52E9163D26A452AB35F7B3B010A371D033B2973A1159249B52A140DB9B74D8400AAA44532E2D7818C3946EA259AED66EB5A87BD0C108D118C91AF5BC71BC5CEE1B75DAA901B47E04E703A0E54C4882D61872B827CF1A2C0F2A59914A5205BC7595DDB45A0689CD6D9C59F22D9A21DFA10F3C87914A8F8833B1BB5C05E0B05C6BEC6463D3083A8455C177ECB5D3653008B24AD43EE32D8ADD23CEEA15F23A92B64CAF7D9682471DC0C4B745F6F2B08D3F6097DCE40F2DB660AADC4CD0E362B1F6497285667E8FF8BDCFDDD6D639F12AAC140BEF75904CDF6D902C48659E58F9B093342F3E4952697AC18198D5AD8288FDE96BC60525E906517E2B4D2E7A0C4AE5FC40ABF8BB1975DE4A64532A000BDEDAF9512A6264B9CC987B572CFF266B47994C3ACE12E32DB45185D5FD2A67512F7101534B19D12BEFA695CFF7A340F6F3AFB18EEE946D6AB553A3CDD21B683E5A432AF911BB064741D459AD7E6CA439B6659DF9C07318A9E47DCB590719A808171AF36EA62523D8005698E9C1F79B27B7C9D1ADD70EE55B1F5E4717DFBCCDA4E7135CAC127BF3E85135AD68011A2348B8C2EFB9D7DA87B7A844E927869B931C5257B0319A41C0935F34CE222D68B6596B7A29F19C93C49F512AEF449CD297CC9378B0C1DD9D6289A0AED1E3374D2A88BEEFE3F18DA54E6F78C59EF1B2711425889CEED4ABEFF2F4809117DE6D6FC6E9199381EA5C630DCB509AF911A70EB6388AE0183345CCBE9712D71A5279B59DE53DAE54EB5FBEEF608B232729E0E215A5823F7BFFDC3CB64C7AABD24F0DB78BE3078FA466FD2CBB418DAE563B37D41A9D4303195DA0D33109B60BC5EAF6546D69AB39085CF579DEF08E739562C7ABE9E34786473E6C1D84671468BD76BA47C607AD60A2CE7E22A6B7CDC10109C7544597484EB7A7CC2B4865B6587C5FECF7012BE027E13252E9FA6D7B7E2F839C20CE5AF323E9164106A572AE37D65C3A3CFB9EB146A71D4816C9B254902F3C5CAE2A8B52962BB55373C4570B058D195BCA34BB4B7CD691B2A1EDAD3D7880C16180761B1BC34E5B1028E5D9E3E9F9815A2E142B1FA3D2C98A54A8026239984B4C7CA79BAD2FE64987B8B54A5BFD00394C1B0F3C5F512AA88331BF11A7D571AE423FDAD9C6234AAA3758150A67F51439FDE87AA6F9713B9C4E9D451313620EBE7A4231218149A838A9467B53F5538FCB44DDD30E4A27B547A47DA01F3CD2FE7162934F229128AA66EDD26AFD128683DF2597CAF58BF813B9CD241569E59295EE5921B0C5DE333AFC2749A5F1C958A7480E8683E928C543A37ACDB3357AEC75EF4D1D4848851CEA88211092D8FA1BFA4421ED58B24596A502908BA207ADB3EA3040B5227FD9263EBF23710821A93C26A5229F5C95547C0F411252811F4845AEB25990CA1A69BE5DBEA4063805E14F0A55FFB293FD46BA3A53C07328A9FC5A9C56C7B94A4F2AC1060A18161A4E39A452ADA462402AB9117B134542419E1C6F4DBC8EEA0F8DF3DA3A07B058EC8D070C9AA26EA351F1047DF486B39657A3E871B9A8FB37928ADF910FE28454B6AAE091848961A4225FFBE2171C95832048752BBD546441A34DD0E4903159B681FF16524163CC941A144E9ED851E116B62556FF6F534BB4479001A9C86240D9F89EB7E9077AD83898281DB612D9970A52D6F31A676ABE7A352953B6513BFE9A8DA2F76785A4A276152595719793CA67E3E2BBCA7CB1FCD1EC49C5AF0035EB88B3417C76359D3482AE9280E73F6C578154DECC2C15E19497C6A532DD2CB999A452AFDE9B556BA74ACC52D3ABAD6EF759C7F1AC8F3242C0C8980A788BA8FF27BB4866691276152595F8AE12EC4B81381352A1152AD4AE72E973FDAB59E2C834FDD8748FA353F5A30B64BD66948AE696EB67E6E809AB997AF3C714AAEA2E592A4A540889FEC4939BBEAD2325D67FDC8255422AE874B97E6C867E54799EA19F9A61D43E9F78DBDE4A645F2A004A1874ABB913430248E81A637AB4C5CB16FC3EF8116BF73CAF26DCC976DA03034011CC9FEC15D3451C487E4457FE30A9AC15B5A952C1A162B97E78ACD83150EC9FA15FA429D7DC4DA2E671C7FF38282DB815462A23FE437CF5B92795637936C24B1D1DAC7A0BA7A24C4E7F8D76618435E67A7AAF3577ACACE32AED7C9958708F37B4B7A3CEE7EF1BBB3F143B8788DD23057D7E8580378BBA5F50FDA1A4C6BF2D0F6018C80AB1EE3907594AED11692FFEB1D5B040AE50198257241285936AA998F1D739D869BBC5C98DE4E4FD4B8867951F3A58777C2BE0F7D5422AE862AA681824768D1027D5565CA71FEC657D14A3F66A14F8A5C5B34A1FAB501ED87CCFC852DA81649156910A4D40CC5EFA819C720CACAF35E03B2D36476A7097BD395EB56E5DC6C77A9CDD77A83635FAD98F8DB2DBA92C3091F386C4A5B2505F12AF271F64EDDD7F8FCFE52A51F1CF9E50831570AE54947D8F2AA0F0366BD394F82BD48D89D7D96981E7308FF5BD1E354F2C91EB2E1EEBC7A4FF9406578A6EB3F66314E8B75E3BD9C71A6920C2D8E8BCF863FD5251F9247DD8A2CA05B7F0B8428F3DD759FBD58B93B854E0AAD7B37269C0C56AB1E377E91FEB51CAE39E348FA91E3768875F0B25953FC11BB09DFF1A7B4D3E9A9775D19BB1BDE0FA36ED4437BB28FEE93B7A0C4AE59BF0064C41537BBDBD62842795B403CBCFB1AB46AACF657142D517462953C10600092A78C8A40709B4A9117BF26DF5D53254CF0C4C09A61CF5B44CAF7982D695A021ADEBB982565908E033B1F8A7F1B5395D05F48838C58F5B8771BCA1CAA60F49D3BEB05220A42B4A0574FDBE7D7091941F3439DB98D62659CCB04290C35F917AC6E86A716AF28ABEE7A3F15788D5FAC19ED6F06B12E71080BE065C6F7DD1522AB8F8D6BD96BB435EACD3BFEC678D6D6905A894DBD8B3DE919F42A2C4EB8C253FA0EE68B252F8134B45BD0123B7C25E355CD0610C73516ECCFC5B2F42DCFACB950AD23AE5B7C669E41AC9AAD58E0DA3290F6616EDDF74EC0DEA751012B752D43EEDC04A25AEB0834117615EA57DD987DEC606E7F2F58843C73FF920D43CD99875A75737992A00C79E351FC7B5B752D4B4F75E6AA782AEC348056E17605355EBF75A7DBBFC083258BB1848FE3DD6C1859E9CDC39C6ECFBA880E00D4B46FD8A28F947302B44DD33740AF76DD1577F3B7957C1C58248AC6E69C2AABC3D6DA470A8ACE016B92DC696A25E61A3164BAD91FAFF2FA9E03A86897DEF04C283E70DDAB9EE365AA23BF0972B15B4E9798BDD3053B6916BE1BE1E46694E4E6779F4EC12C919F088B9BE247EB75A3F39CC1E718DE3CF7DD788BD8F4E29541997CAF5DD9DAD11D7D28B76D8BE76A3BD6DB43CB8C3B046EC2B487CE134530560260A3B98544398A11AFD506F6B40868D052D835239FB8E8D1E73E32766802957B3DBF33EF3348A060E2BF54BABF41D9DAC916DBC08DF8AE40CED681C9843B3AEC6DEF8AA3D30DE237E0EBCD338BDDF1BF8F945FA911ED69036B0225B64A6EF2FE21FA104A44256BF34BC8DA5466F9EA7AF7F96BED882033DCE6CE0932EE2289489B123A475A2E2091BF50DC3E0E87CB2209574CF2A40693E9354E0A4573B2BF175A7F54689748B1E2FFFAC02B0C4A49DB26CF167930A400A86BD64D07900654735AA9D9BE3BD69393A553FB914858299D0E91BA92B4DF55D20F22CA15AEC28E84BFB6880125FA39F9EA51F85ED34FDF05C253052CBA579E6A737260C2F5301DD22F64ECC315C5569173E3516DE93F8378120E8372115C4BC8C3AF55EC54CD74F7CA637E55B632C2A05D4047D98D88448A87450135E8418DD14F9D0AFAA61BB7662883546568F0A123F11F0D8FF3269746800DB4AFD946F3B4D3FAE9E82E036FE060C5191D5D5CEFC713ABD01C72D385FA59F28F36283D539B852DBE61EB1F365FAE61866C4CF67125F572A0820E90D1828C34F73A6FC1A4126A9A8AE3FED6434D37749E9BB6A475EA6B7A32DA4824E836FC02427E6886D9DEC0FE5F7F6FD38B34B2B4A65E528B9B4AFD32E145B83D34905CD7031FF46FB546394FED1053521A5E58179828A0E44DD0F69E54E9D574C4C9F9FC9FF9AC0121BB485210A625FD45D614E682930AA80523997107099987B67A2023079C5AF0B9A7ED86ED4F7C69F8BD45D1FCC59F14F4C7A8984EEA4AEBC4E15309F624CBBC3EB143EFBE059EB78941EB79246074384BD433BF47B7AB1AB6ADDEF05BFC3C3B0FBCD43DBE25FE04FB2C518B7273E57417B05AAB0E415C33D12A548D02C299F08A34E543E45E7A2A41E930826EAC24C31EFAE14A9608DDB220755216A9F4948657291FC900099C12DF4E8F70E500CB5C6FCB6E40A99A12FE1AB665BB43D1D135281F37E0FD83B67CBF50E77F71A1FC971157DDF5E373B90F6E0D0C056FDD03BF6C86BC9B31F677669CD5D65B85C96F0B430D01A1C4D2F77B4446ABAD2666AED464DC8CF771518FFE9023B57EEB399E6153E3B477286FCA3D98C9D2761ABB98DE6A848CE9B2D7502A80226798BE5B9A9624EFC19468143DD8E46F841EAF523C5E647224DDEC9DBAD76F5787A0EF143F5C1608F0DB026C43C43344684386E15B533BFC032196CB95EDBFB5CECB7F2C0967674B80227C84C7E3B7B2316781C697CDB0AEDE22C63D13DF4D60845EFDBE2178021BF11713E7AD63C89920D98B88BF5B50FC57E279F6A544BBFAF541289AAD64E8CB32645C9AD7F97A4729F751A23C202BF3AB1AB60B0FD7F2530343FD420D4B8DC98DBD6DB55267430BEA48FFCE9D4B0FB85161F0475C7C6D2599C8550D160BB587E33A9EBBDB6F6B2611452925B091D62F7E7DB23DBA42FB3ACD02A5201481C86A7BE9FA3F684A40649A03D8A46B5F78121CDCA959025956C1B9CDA207E543009EA4481095377114CAA4E14081505AA9AA58298530D31FCD4D1F9EBE8E5410650DC49B6083EB8CCA782E1239224934C0FD969094E5F6A321195EF3F28578C545D4C4B30E7C8929F93A04E1468E6CFA91A69B0A2D2026FADA713D05A5261986F182C158609054B856142C152619850B0541826142C158609054B856142C152619850B0541826142C158609054B856142C152619850B0541826142C158609054B856142C152619850B0541826142C158609054B856142C152619850B0541826142C158609054B856142C152619850B0541826142C158609054B856142C152619850B0541826142C158609054B856142C152619850B0541826142C1586094124F67F18DE40020F807B860000000049454E44AE426082}\par }
{\pard {\line}{\line}{\b As per example given, our static text starts from here!! }{\line}
This is an example of how the word file will look like whence generated. The image will be at the top and at the center of the Page.{\line}Below that, there will be one paragraph where the first line will be of style bold and rest of the text will be in regular style with font family as Helvetica and font size as 12pt.
\par }}

This may seems to be a mess but it isn’t.
Let’s decode the above text which consist the data and RTF Code:

  • { – Marks the starting of document, property, etc.
  • RTF – represents the file format like we do for HTML or XML.
  • Ansi – Character Encoding
  • {\pard \qc – This marks start of paragraph with alignment as center (qc is for center alignment).
  • \pict\pngblip – pict represent that we are going to add a picture in our document and pngblip means it’s of type png.
  • Long paragraph which seems to be a bag of random number and alphabets, is actually the image in HEX code. You can easily convert any image or file in hex using online tools. I will provide the links at the end.
  • \par } – This refers to end of paragraph. In our case, we are ending a center aligned paragraph which consist an image.
  • {\line} – For linefeed. It’s like <br> of html.
  • {\b text }– to bold the text which is in the scope of these brackets

ABAP Code:

We can use the above FM, to have our text in lt_lines internal table and using this, we can manipulate it as required to incorporate dynamic text. At the end, we can use this internal table, to download the file on our system, or share it using email via ABAP, etc. keeping the format/extension as ‘.doc’/’.docx’/’.rtf’.

Below is the screenshot of final output generated by downloading the above internal table to system using extension as ‘.doc’:

Conclusion

Using this blog, developer can generate a word file with static or dynamic data within a predefined template and icing on the cake is, you have the versatile formatting and styling option with an ease of adding an image.

 

Important Links:

Note:

  • I am not the owner of any of the website mentioned above. Please follow the policies as stated by the website’s owner with your due diligence.
  • I am the owner of all the content used in this blog.
2 Comments
You must be Logged on to comment or reply to a post.