I am trying to get all lines from the header and footer in a word doc. I am using the following code:
HeaderFooter header = this.Doc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
string text = header.Range.Text;
But the Range.Text
property always seems to return the last line. If my header has:
Header line 1
Header line 2
Header line 3
My code always return "Header line 3". I get similar results for the footer. I have tried calling header.Range.WholeStory()
. I have tried calling header.Range.Paragraphs
and tried iterating over the Paragraphs
collection. I am at a complete loss. The documentation on MSDN is cryptic and I can't find my way through it. Any help would be appreciated.
FWIW: I am writing a utility in C# to verify that each document a large library of word documents conforms to a company wide template. The template calls for the header and footer to contain certain information (title, document number, date, etc). Each datum will also be checked against a regex. I know the use of fields, bookmarks, etc. would be elegant but I am afraid the documents I am working with simply have the information embedded as text in the header and the footer.