-2

I succesfully did Search Replace in doc docx header and footers using: this question. I am using Delphi to control Word via OLE Automation.

Now the problem is that that works only for first page in case one sets "different header for first page" or doesn't work at all if one sets "different on odd/even".

This document explains it a little more.

UPDATE:

That document linked above explains that MS Word manages header & footer as first page / Odd pages / even pages. But it is also possible to choose no one option and in this case all header and footer are the same.

IN case one does search and replace using the code I linked above it works only in case no extra options are selected, so in the case one is serachin and replacing in a document where all header and footers are the same.

If by chance that code is used against a document that has "differente header for first page" selected, serach and replace will occur only on header and footer of first page.

While it won't work at all if the only option selected is "different header and footer on odd and even pages" (and no "different on first page").

So I was asking if one already solved this problem looping coorectly through all cases. I mean, it would be useful to find this answer also for other users that need to do search and replace in header and footers in MS word docs, using Delphi and OLE automation.

Community
  • 1
  • 1
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
  • To mr. "let's close this question": THIS IS a question, read again the title, and it is an important questino also for all the people that think that having implemented a search replace algorithm using the link i mentioned above they are safe. You are not safe because if you are trying to access headers and footers taht are different than in the first page or in odd/even pages you won't succeed. – UnDiUdin Dec 01 '11 at 09:53
  • 1
    I think the downvoters feel that "Did anyone has the code for it?" doesn't show a real question with enough of effort to find a solution yourself. – GolezTrol Dec 01 '11 at 10:17
  • Also, the document you link to has nothing to do with it. It's about replacing entire headers by digging directly into the document's XML. – GolezTrol Dec 01 '11 at 10:20

1 Answers1

1

The answer in the other questions jumps to the header of the current page, as you can clearly tell by the name. If you're on the first page of a section, that means you will change that different header only. There are other views you can jump to to find other parts of the document:

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdseekview(v=office.11).aspx

Also note that a document can contain multiple sections that can each have a separate header. In that case, you should iterate through them and repeat the steps for each header.

If a section has only one page, it may be hard to jump to the default header if the section has a different header for its first page. In that case you may need to swith off the 'different first page' flag, modify the header, and switch it on again.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Basically I accepted the answer to stop the downvotes. Ok I will unselect the answer and try to express myself again updating the question. – UnDiUdin Dec 01 '11 at 15:35
  • @WarrenP The question isn't really clear, but the question it links to provides some more context. That is indeed a programming question. The fact that is has a Delphi tag suggests OLE Automation. At least, to me. I've been automating Word for about 10 years since Word 95, using the old Word API, OLE automation and VBA. So maybe that made it a little easier to guess what the problem was. – GolezTrol Dec 02 '11 at 09:14
  • The document which is linked shows how to replace entire headers. It indeed shows that there are multiple headers in case you got different First Page headers and/or different odd/even headers, but the code example itself doesn't use automation (or search&replace for that matter). Instead it just proddes in the document XML structure to replace the headers there. .docX files are actually zip-files with XML content in them. It is a completely different technique and it's not possible to use that on .doc files. – GolezTrol Dec 02 '11 at 09:23
  • I fixed the question to add a bit of a grounding point (We're using Delphi, and OLE Automation) – Warren P Dec 02 '11 at 19:44