1
votes

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.

2
I haven't posted on SO in several years. Good to know some things haven't changed. Thanks unhelpful person for down-voting my question without giving a reason. I obviously didn't really want help or expect anyone to answer. Thanks for voting to close my question also. Clearly you know better than me what is worth asking and what isn't. SO contians a wealth of information and I always laugh when the 1st google hit is an SO question that was closed for some pedantic reason yet has the very answer I was seeking. Thanks down-voter! You are doing the Lord's work.dFlat

2 Answers

1
votes

You can try the rest of the headers just in case

foreach (Section section in this.Doc.Sections)
    foreach (HeaderFooter header in section.Headers)
        if (header.Range.Text.Contains("1")) Debugger.Break();
0
votes

I found the answer. Like most things it was hidding in plain sight. I was debugging from the console using Console.WriteLine() and the slash Rs were getting the best of me.

private string GetUsableTextFromRange(Word.HeaderFooter headerFooter)
{
    Word.Range range = headerFooter.Range;
    string textWithSlashRs = range.Text;
    string usableText = textWithSlashRs.Replace("\r", Environment.NewLine);
    return usableText;
}

In the end I had the data all along... Hope this helps someone else.