Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert a Word document to text or html in an ABAP program

evertdutoit
Explorer
0 Kudos

Hi,

At my client's site, for the recruitment system, they have the word processing system set to RTF, instead of SAP Script. This means that all the correspondence is in Word format. A standard SAP program takes the word letter, loads word, does the mail merge with the applicant's info and then sends the document to a printer.

The program name is RPAPRT05. The program creates a document proxy (interface I_OI_DOCUMENT_PROXY) and manipulates the document using the methods of the interface.

Now what we want to do is to instead of sending the document to a printer, we want to email the document contents to the applicant. But I don't know how to get the content from the Word document into text or html format so that I can make an email from it.

I know I can send an email with the word document as an attachment, but we'd prefer not to do that.

I would appreciate any help very much.

Thanks

11 REPLIES 11

Former Member
0 Kudos

Evert,

Func mod:

CONVERT_RTF_TO_ITF

or

SO_RTF_TO_HTM

depending on which output you want.

Don't forget those points.

0 Kudos

since your form is in RTF format using FM SO_RTF_TO_HTM convert it to htm format and then use the following code sample (you need a slight modification to remove submitting of programs, etc and just pass the htm table to the method) to email the report

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/5931ff64-0a01-0010-2bb7-ff2...

Regards

Raja

0 Kudos

I'm still trying to work this out. It turns out the document is stored as a Word template with mail merge fields. All that doesn't translate to HTML well.

Initially I thought that if you save the word document after the mail merge were completed, it'll save it without all that stuff, but alas, it keeps the mail merge info in the document. Now I'm trying to work out the whole translating to SAPScript idea. I'll post again when I've made more progress.

Luke, yeah, I'm originally from South Africa, but I've been in the States almost 9 years now.

0 Kudos

Ho gan dit? (did i spell it right?)

Raja

0 Kudos

Hi Raja, nice try Hoe gaan dit?

It's going great, except for SAP driving me up the wall.

Sorry, I can't even try replying in your language.

0 Kudos

Hi Evert,

Aaah thats awesome. I am in New Zealand at the moment, but looking to perhaps come and work in the US for awhile, so if you can send me some info that would be great. lukejvisser@gmail.com

So are you now developing SAPscripts or trying to convert the word doc to a SAPScript?

Ja boet ek wet nie hoekom SAP nie iets al nou in gebou vir hierdie donders se ding.

See Ya

Luke

0 Kudos

Hi Luke, email me at edutoit AT us.epiuse.com ... our company is always looking for people with HR experience.

0 Kudos

oh yeah, and right now I'm looking at converting the Word doc to SAPScript. Unfortunately I'm having some other difficulties first

0 Kudos

Hi Evert DuToit ,

Thanks for correcting .

Regards

Raja

Former Member
0 Kudos

Hi Evert,

How you doing today?

I have just two questions?

1) Did you get this right? As i have the exact same request. Cannot believe that SAP have not addressed this yet.

2) Are you from SA?

See Ya

Luke

evertdutoit
Explorer
0 Kudos

Ok, here's what I ended up doing:

First of, in order to call FM 'CONVERT_RTF_TO_ITF' you need the RTF document in a table with line length 156. The document is returned from FM 'DP_CREATE_URL' in a table with line length 132. So first I convert the table:

  • Transform data table from 132 character lines to

  • 256 character lines

LOOP AT data_table INTO dataline.

IF newrow = 'X'.

  • Add row to new table

APPEND INITIAL LINE TO xdatatab ASSIGNING .

newrow = space.

ENDIF.

  • Convert the raw line of old table to characters

ASSIGN dataline TO .

  • Check line lengths to determine how to add the

  • next line of old table

newlinelen = STRLEN( newline ).

ADD addspaces TO newlinelen.

linepos = linemax - newlinelen.

IF linepos > datalen.

  • Enough space available in new table line for all of old table line

newline+newlinelen = oldline.

oldlinelen = STRLEN( oldline ).

addspaces = datalen - oldlinelen.

CONTINUE.

ELSE.

  • Fill up new table line

newline+newlinelen(linepos) = oldline(linepos).

ASSIGN newline TO .

newrow = 'X'.

  • Save the remainder of old table to the new table line

IF linepos < datalen.

oldlinelen = STRLEN( oldline ).

addspaces = datalen - oldlinelen.

CLEAR newline.

newline = oldline+linepos.

ELSE.

CLEAR newline.

ENDIF.

ENDIF.

ENDLOOP.

  • Write the last line to the table

IF newrow = 'X'.

APPEND INITIAL LINE TO xdatatab ASSIGNING .

Next I call FM 'CONVERT_RTF_TO_ITF' to get the document in SAPScript format:

  • Convert the RTF format to SAPScript

CALL FUNCTION 'CONVERT_RTF_TO_ITF'

EXPORTING

header = dochead

x_datatab = xdatatab

x_size = xsize

IMPORTING

with_tab_e = withtab

TABLES

itf_lines = itf_table

EXCEPTIONS

invalid_tabletype = 1

missing_size = 2

OTHERS = 4.

This returns the document still containing the mail merge fields which needs to be filled in:

LOOP AT itf_table INTO itf_line.

WHILE itf_line CS '«'.

startpos = sy-fdpos + 1.

IF itf_line CS '»'.

tokenlength = sy-fdpos - startpos.

ENDIF.

token = itf_line+startpos(tokenlength).

REPLACE '_' IN token WITH '-'.

ASSIGN (token) TO .

ENDIF.

MODIFY itf_table FROM itf_line.

ENDWHILE.

ENDLOOP.

And finally I use FM 'CONVERT_ITF_TO_ASCII' to convert the SAPScript to text. I set the line lengths to 60, since that's a good length to format emails to.

  • Convert document to 60 char wide ascii document for emailing

CALL FUNCTION 'CONVERT_ITF_TO_ASCII'

EXPORTING

formatwidth = 60

IMPORTING

c_datatab = asciidoctab

x_size = documentsize

TABLES

itf_lines = itf_table

EXCEPTIONS

invalid_tabletype = 1

OTHERS = 2.

And then the text document gets passed to FM 'SO_NEW_DOCUMENT_ATT_SEND_API1' as the email body.