Context: MS Word automation with interop, c#, a document with 2000 sections and 3 headers.
Problem: efficient processing of headers and footers (e.g. text search & replace in a header)
It seems the way to process headers (or footers) in a word is something like this:
foreach (Microsoft.Office.Interop.Word.Section section in theDoc.Sections)
{
foreach (Microsoft.Office.Interop.Word.HeaderFooter header in section.Headers)
{
//processRange(theDoc, header.Range);
}
}
Problem is the loop will run thousand of times for this document I bumped into even though it has only one header. Since section can actually have different headers, obviously, I can't just stop after the first hit on a header. But in this case, that's the objective since there is only 3 headers, not 6000.
I was thinking about what makes a header unique so I can keep keys in a hashset and just skip if already there.
What is that key, if there is one? range.Start+range.End+range.StoryType (range of headerfooter)? range.text ? Something else ?
Is there perhaps a better approach to avoid the redundancy?
Thanks for your help.
-Cristian