我希望使用Microsoft Office Interop Word Assemblies来读取Word文档的页眉和页脚。
我的问题有两个:
第一个问题是如何访问脚注和页眉? 第二个问题是如何将它们转换为字符串(当我调用toString()时,只得到"System.__ComObject")?
我的问题有两个:
第一个问题是如何访问脚注和页眉? 第二个问题是如何将它们转换为字符串(当我调用toString()时,只得到"System.__ComObject")?
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);
}
如果您对注脚感兴趣而不是页脚(由于您在某些地方写了注脚,而在其他地方写了页脚,因此这并不是非常清楚的问题),那么事情就更简单了,因为您可以要求文档收集所有它的注脚。要访问备注中的文本,您可以执行与标题/页脚相同的操作:访问范围,然后获取文本属性:
List<string> footNotes = new List<string>();
foreach (Footnote aNote in app.ActiveDocument.Footnotes)
footNotes.Add(aNote.Range.Text);