How to replace text in the header of a Word-document (.docx) - Forum - OpenEdge General - Progress Community

How to replace text in the header of a Word-document (.docx)

 Forum

How to replace text in the header of a Word-document (.docx)

This question is answered

Hello,
I try to search and replace text in Word-documents.
With the text below, it works to search and replace normal text in the documents, but it doesn't work for text in the page header and page footer of the document.
Can somebody help me to let it work for pageheaders?

DEFINE VARIABLE chWordApplication AS COM-HANDLE NO-UNDO.

CREATE "Word.application" chWordApplication.
chWordApplication:Documents:Open("c:\temp\test.docx").
chWordApplication:Selection:find:text = "Old".
chWordApplication:Selection:find:replacement:text = "New".

chWordApplication:Selection:find:Execute(,,,,,,,,,,2).

chWordApplication:ActiveDocument:SaveAs("C:\temp\Test2.docx").
chWordApplication:Quit().

Kind regards,
Richard

Verified Answer
  • You'll need to do something like the code below as your go between header, main body and footer.

    chDocument:ActiveWindow:ActivePane:View:SeekView = 9. /* The current page header.*/

    chDocument:ActiveWindow:ActivePane:View:SeekView = 10. /* The current page footer.*/

    chDocument:ActiveWindow:ActivePane:View:SeekView = 0. /* MainDocument */

All Replies
  • You'll need to do something like the code below as your go between header, main body and footer.

    chDocument:ActiveWindow:ActivePane:View:SeekView = 9. /* The current page header.*/

    chDocument:ActiveWindow:ActivePane:View:SeekView = 10. /* The current page footer.*/

    chDocument:ActiveWindow:ActivePane:View:SeekView = 0. /* MainDocument */

  • Thank you very much.

    This is indeed the solution.

    I've changed my code to this, and it works perfect.

    DEFINE VARIABLE chWordApplication AS COM-HANDLE NO-UNDO.

    CREATE "Word.application" chWordApplication.

    chWordApplication:Documents:Open("j:\temp\test.docx").

    chWordApplication:Selection:find:text = "Old".

    chWordApplication:Selection:find:replacement:text = "New".

    chWordApplication:ActiveDocument:ActiveWindow:ActivePane:View:SeekView = 9. /* select header */

    chWordApplication:Selection:find:Execute(,,,,,,,,,,2).  /* replace */

    chWordApplication:ActiveDocument:ActiveWindow:ActivePane:View:SeekView = 10. /* select footer */

    chWordApplication:Selection:find:Execute(,,,,,,,,,,2).  /* replace */

    chWordApplication:ActiveDocument:ActiveWindow:ActivePane:View:SeekView = 0. /* select main */

    chWordApplication:Selection:find:Execute(,,,,,,,,,,2).  /* replace */

    chWordApplication:ActiveDocument:SaveAs("C:\temp\Test2.docx").

    chWordApplication:Quit().

    Kind regards,

    Richard