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: 

ABAP - OLE MS-WORD (save .docx as PDF)

0 Kudos

Hi Geeks,

I have a requirement on manipulating a MS-word (.docx) document through OLE (Object Link Enabling) and saving as PDF in the local system.

I have used WORD.BASIC as the object and managed to manipulate the document (.docx) and save it in the same .docx format.

But I am facing technical challenge in saving the document (.docx) as PDF.

can anyone guide me how to save the .docx to PDF through OLE. I have browsed a lot of blogs and forums but cudnt get a proper solution to save the manipulated document (.docx) as PDF.

PFB. the code i used for manipulation of the document (.docx).


    CREATE OBJECT lv_word 'WORD.BASIC'.

    CALL METHOD OF lv_word 'FILEOPEN' EXPORTING #1 = <<file_path>>.

    CALL METHOD OF lv_word 'ViewHeader'.
    CALL METHOD OF lv_word 'EDITREPLACE'
    EXPORTING
      #01 = <<Find_Word>>
      #02 = <<Replace_word>>
      #03 = 0
      #04 = 0
      #05 = 0
      #06 = 0
      #07 = 0
      #08 = 0
      #09 = 0
      #10 = 1
      #11 = 0
      #12 = 1.
    CALL METHOD OF lv_word 'CloseViewHeaderFooter'.
    CALL METHOD OF lv_word 'FILESAVEAS' EXPORTING #1 = <<file_path>>.
    CALL METHOD OF lv_word 'DOCCLOSE' EXPORTING #1 = 2.
    CALL METHOD OF lv_word 'APPCLOSE'.

Thanks & Regards,

Saivignesh Sudarsan.

1 ACCEPTED SOLUTION

0 Kudos

Hi Geeks,

Thanks for your response i was able to get an idea from both of u (Simone Milesi & Dieter Gröhn) .

but i was not sure how to use the parameters of the methods to save the .docx file as pdf.

so upon my search, found a thread (given below) and was helpuful. 

                             

Now let me brief how i was able to achieve the goal ( saving .docx file to pdf format using OLE).

Step - 1:

Initially i used the WORD.BASIC for manipulating the .docx file.

PFB.The code i used (as mentioned earlier)

    CREATE OBJECT lv_word 'WORD.BASIC'.

    CALL METHOD OF lv_word 'FILEOPEN' EXPORTING #1 = <<file_path>>.

    CALL METHOD OF lv_word 'ViewHeader'.
    CALL METHOD OF lv_word 'EDITREPLACE'
    EXPORTING
      #01 = <<Find_Word>>
      #02 = <<Replace_word>>
      #03 = 0
      #04 = 0
      #05 = 0
      #06 = 0
      #07 = 0
      #08 = 0
      #09 = 0
      #10 = 1
      #11 = 0
      #12 = 1.
    CALL METHOD OF lv_word 'CloseViewHeaderFooter'.
    CALL METHOD OF lv_word 'FILESAVEAS' EXPORTING #1 = <<file_path>>.
    CALL METHOD OF lv_word 'DOCCLOSE' EXPORTING #1 = 2.
    CALL METHOD OF lv_word 'APPCLOSE'.

Step - 2:

Now,I used WORD.APPLICATION to save the manipulated file to pdf.

     create object  lv_wordapp 'word.application'.

     set property of lv_wordapp 'Visible' = 0.

     call method of lv_wordapp 'Documents' = v_worddoc.

     call method of lv_worddoc 'Open'

          exporting

               #1 = 'C:\  <FILENAME.DOCX>'.

     call method of lv_wordapp 'ActiveDocument' = lv_wordadoc.

     call method of lv_wordadoc 'Content' = lv_wordcont.

     call method of lv_wordadoc 'SaveAs'

          exporting

               #1 = 'C:\  <FILENAME.PDF>'

               #2 = 17.

     call method of lv_wordapp 'Quit'.

     free object lv_wordapp .

The PDF file gets generated successfully.

So finally got a solution and the above code works perfectly fine

Thanks everyone

Thanks & Regards,

Saivignesh Sudarsan.

6 REPLIES 6

SimoneMilesi
Active Contributor
0 Kudos

Hi!

When you invoke SaveAs Method, pass as 2nd parameter '17' ->it's the value for saving as pdf

i got it from another forum


Alrighty, so I figured it out.

It turns out 17 is the FormatType for PDF, so you just have to do

something like this:

document.SaveAs(filename, 17)

I didn't see any documentation about this, but we just started going up

from 0 as a FormateType, and once we hit 17, it made a valid PDF!!

The reference is from a Ruby blog but it uses Win OLE to achieve it, so i think you can do the same

Ruby on Windows: Saving Microsoft Office Documents as PDFs

Let me know if it works

0 Kudos

Hi Simone Milesi,

Thanks a ton for your most important piece of information from which i have derived the solution for saving the .docx to pdf format.

Feeling thankful & Great

Thanks & Regards,

Saivignesh Sudarsan.

Former Member

Hi,

you can export the DOC via OLE.

Try this:

Regards, Dieter

0 Kudos

This works fine.

0 Kudos

Hi Geeks,

Thanks for your response i was able to get an idea from both of u (Simone Milesi & Dieter Gröhn) .

but i was not sure how to use the parameters of the methods to save the .docx file as pdf.

so upon my search, found a thread (given below) and was helpuful. 

                             

Now let me brief how i was able to achieve the goal ( saving .docx file to pdf format using OLE).

Step - 1:

Initially i used the WORD.BASIC for manipulating the .docx file.

PFB.The code i used (as mentioned earlier)

    CREATE OBJECT lv_word 'WORD.BASIC'.

    CALL METHOD OF lv_word 'FILEOPEN' EXPORTING #1 = <<file_path>>.

    CALL METHOD OF lv_word 'ViewHeader'.
    CALL METHOD OF lv_word 'EDITREPLACE'
    EXPORTING
      #01 = <<Find_Word>>
      #02 = <<Replace_word>>
      #03 = 0
      #04 = 0
      #05 = 0
      #06 = 0
      #07 = 0
      #08 = 0
      #09 = 0
      #10 = 1
      #11 = 0
      #12 = 1.
    CALL METHOD OF lv_word 'CloseViewHeaderFooter'.
    CALL METHOD OF lv_word 'FILESAVEAS' EXPORTING #1 = <<file_path>>.
    CALL METHOD OF lv_word 'DOCCLOSE' EXPORTING #1 = 2.
    CALL METHOD OF lv_word 'APPCLOSE'.

Step - 2:

Now,I used WORD.APPLICATION to save the manipulated file to pdf.

     create object  lv_wordapp 'word.application'.

     set property of lv_wordapp 'Visible' = 0.

     call method of lv_wordapp 'Documents' = v_worddoc.

     call method of lv_worddoc 'Open'

          exporting

               #1 = 'C:\  <FILENAME.DOCX>'.

     call method of lv_wordapp 'ActiveDocument' = lv_wordadoc.

     call method of lv_wordadoc 'Content' = lv_wordcont.

     call method of lv_wordadoc 'SaveAs'

          exporting

               #1 = 'C:\  <FILENAME.PDF>'

               #2 = 17.

     call method of lv_wordapp 'Quit'.

     free object lv_wordapp .

The PDF file gets generated successfully.

So finally got a solution and the above code works perfectly fine

Thanks everyone

Thanks & Regards,

Saivignesh Sudarsan.

Former Member
0 Kudos

I would add the following line to this code before quitting the application:


call method of

     lv_wordadoc

     'Save'.

This would avoid the word application to prompt for save before closing the docx file.


Thanks for sharing!