Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

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

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.

Former Member
Former Member replied

Hi Geeks,

Thanks for your response i was able to get an idea from both of u (@Simone Milesi & Former Member) .

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. 

                              SaveAs in OLE

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.

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question