从Word.Range读取页眉和页脚 – VSTO Word Interop C#

我有一个Range对象,我想用它从Range所在的页面中提取信息.信息在表格的页眉和页脚中,我想从表格中读取文本.

我试过:word.Sections [1] .Headers [WdHeaderFooterIndex.wdHeaderFooterPrimary] .Shapes.Range(ref pageNumber).TextFrame.TextRange.Text;

其中word是Range,pageNumber是此范围所在的页码.
问题是我无法获得真正的页码!使用word.get_information(WdInformation.wdActiveEndPageNumber)返回错误的页码!

最佳答案
我找到了任何1感兴趣的答案:

int i = (int)range.get_Information(WdInformation.wdActiveEndPageNumber) % 2;
WdHeaderFooterIndex index;
if (i == 0 && range.Sections[1].PageSetup.OddAndEvenPagesHeaderFooter == 1)
    index = WdHeaderFooterIndex.wdHeaderFooterEvenPages;
else
    index = WdHeaderFooterIndex.wdHeaderFooterPrimary;

Range sRange = range.Sections[1].Range;
object direction = Word.WdCollapseDirection.wdCollapseStart;
sRange.Collapse(ref direction);
if (range.get_Information(WdInformation.wdActiveEndPageNumber) == sRange.get_Information(WdInformation.wdActiveEndPageNumber)
    && range.Sections[1].PageSetup.DifferentFirstPageHeaderFooter == 1)
    index = WdHeaderFooterIndex.wdHeaderFooterFirstPage;

object rangeIndex = 1;
Range headerRange = range.Sections[1].Headers[index].Range.ShapeRange.TextFrame.TextRange;

string profession = headerRange.Tables[1].Cell(4, 1).Range.Text;
string manPower = headerRange.Tables[1].Cell(4, 2).Range.Text;
string registration = headerRange.Tables[1].Cell(4, 3).Range.Text;
string taggingListNum = headerRange.Tables[1].Cell(4, 4).Range.Text;

转载注明原文:从Word.Range读取页眉和页脚 – VSTO Word Interop C# - 代码日志