0

我试图从文档中的表格单元格中引用现有的结束注释(使用 netoffice):

row.Cells[2].Range.InsertCrossReference(WdReferenceType.wdRefTypeEndnote, WdReferenceKind.wdEndnoteNumberFormatted, 1);

但是,这似乎将引用放在行的开头,而不是 Cell[2] 的文本末尾。一般来说,我在网上没有找到太多关于如何以编程方式添加对脚注和尾注的交叉引用的帮助。如何获得正确显示的参考?

4

1 回答 1

1

问题是Range您的代码片段中指定的目标是整个单元格。您需要“折叠”Range才能在单元格内。(将范围想象为选择。如果单击单元格的边缘,则会选择整个单元格,包括其结构元素。如果然后按左箭头,则选择会折叠成闪烁的工字梁。)

要转到单元格的开头:

Word.Range rngCell = row.Cells[2].Range;
rngCell.Collapse(Word.WdCollapseDirection.wdCollapseStart);
rngCell.InsertCrossReference(WdReferenceType.wdRefTypeEndnote, WdReferenceKind.wdEndnoteNumberFormatted, 1);

如果单元格有内容并且您希望它位于内容的末尾,那么您可以使用它wdCollapseEnd。棘手的部分是这会将目标点放在下一个单元格的开头,因此必须将其移回一个字符:

Word.Range rngCell = row.Cells[2].Range;
rngCell.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
rng.MoveEnd(Word.WdUnits.wdCharacter, -1);
rngCell.InsertCrossReference(WdReferenceType.wdRefTypeEndnote, WdReferenceKind.wdEndnoteNumberFormatted, 1);
于 2016-04-20T05:44:34.983 回答