简体   繁体   English

Microsoft Office Interop Word读取标题和脚注

[英]Microsoft Office Interop Word read header and footnote

I want to use Microsoft Office Interop Word Assemblies to read header and footers of word documents. 我想使用Microsoft Office Interop Word Assemblies来读取word文档的页眉和页脚。

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()) 第二个如何将它们转换为String(当我调用toString()时,我只得到了“System .__ ComObject”)

You should have a Document object doc which is composed of many Sections, and the footers/headers are part of the single sections. 您应该有一个Document对象doc,它由许多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. 要访问页眉/页脚的文本,您必须获取页眉/页脚中包含的范围,然后访问其Text属性。

If app is your Word ApplicationClass, this code should give you two collections with the headers and footers of the active document: 如果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);
        } 

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: 要访问注释中的文本,您可以执行与页眉/页脚相同的操作:访问Range,然后获取Text属性:

        List<string> footNotes = new List<string>();
        foreach (Footnote aNote in app.ActiveDocument.Footnotes)
            footNotes.Add(aNote.Range.Text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM