Reputation: 1041
I want to use Microsoft Office Interop Word Assemblies to read header and footers of word documents.
I have two problems :
First how to access footnotes and headers? Second how to convert them to String (i just got "System.__ComObject" when i call toString())
Upvotes: 4
Views: 8529
Reputation: 7468
You should have a Document object doc which is composed of many Sections, and the footers/headers are part of the single sections. Each section can have multiple headers/footers (they can for instance be different for the first page). To access the text of the header/footer you have to get the Range contained in the header/footer, and then access its Text property.
If app is your Word ApplicationClass, this code should give you two collections with the headers and footers of the active document:
List<string> headers = new List<string>();
List<string> footers = new List<string>();
foreach (Section aSection in app.ActiveDocument.Sections)
{
foreach (HeaderFooter aHeader in aSection.Headers)
headers.Add(aHeader.Range.Text);
foreach (HeaderFooter aFooter in aSection.Footers)
footers.Add(aFooter.Range.Text);
}
If you are interested in footnotes instead of footers (it's not really clear from the question, since you wrote footnotes in some places, and footers in others), things are even simpler, since you can ask a document the collection of all it footnotes. To access the text inside the note you can do the same seen for headers/footers: access the Range and then get the Text property:
List<string> footNotes = new List<string>();
foreach (Footnote aNote in app.ActiveDocument.Footnotes)
footNotes.Add(aNote.Range.Text);
Upvotes: 6