我想使用Microsoft Office Interop Word Assemblies来读取word文档的页眉和页脚.
我有两个问题:
首先如何访问脚注和标题?第二个如何将它们转换为String(当我调用toString()时,我只得到了"System .__ ComObject")
您应该有一个Document对象doc,它由许多Sections组成,而页脚/标题是单个部分的一部分.每个部分可以有多个页眉/页脚(例如,它们可以与第一页不同).要访问页眉/页脚的文本,您必须获取页眉/页脚中包含的范围,然后访问其Text属性.
如果app是您的Word ApplicationClass,此代码应该为您提供两个包含活动文档的页眉和页脚的集合:
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);
}
Run Code Online (Sandbox Code Playgroud)
如果你对脚注而不是页脚感兴趣(问题不是很清楚,因为你在某些地方写过脚注,而在其他地方写了脚注),事情甚至更简单,因为你可以向文档提出所有脚注的集合.要访问注释中的文本,您可以执行与页眉/页脚相同的操作:访问Range,然后获取Text属性:
List<string> footNotes = new List<string>();
foreach (Footnote aNote in app.ActiveDocument.Footnotes)
footNotes.Add(aNote.Range.Text);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6650 次 |
| 最近记录: |