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

Posted by richardz on 08-Nov-2017 08:18

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

Posted by tbergman on 08-Nov-2017 10:03

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

Posted by tbergman on 08-Nov-2017 10:03

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 */

Posted by richardz on 09-Nov-2017 02:02

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

This thread is closed