[英]C# interop: How to determine a range of a Field?
I'd like to loop through all the fields in my document and place text after those of type wdFieldIndexEntry but Fields don't have a Range value they way Bookmarks do. 我想遍历文档中的所有字段,并将文本放在wdFieldIndexEntry类型的字段之后,但是字段没有“书签”所能实现的Range值。
Below is the closest I've come: 以下是我最近来过的:
foreach( Field f in document.Fields)
{
if( f.Type == WdFieldType.wdFieldIndexEntry)
{
// f.Range.InsertAfter("{{Some After text}}"); // <- no Range field
f.Code.InsertAfter("{{Some After text}}"); // puts text inside field
}
}
As noted the above puts the text in the code (not surprisingly). 如前所述,上面将文本放入代码中(毫不奇怪)。 How do I get the field location/Range so I can insert text before or after the field?
如何获取字段位置/范围,以便可以在字段之前或之后插入文本?
Actually, fields do return Range
objects. 实际上,字段确实返回
Range
对象。 Most field types can return two kinds of ranges: one for Field.Code
, another for Field.Result
. 大多数字段类型可以返回两种范围:一种用于
Field.Code
,另一种用于Field.Result
。
The Index field is special in that it returns only Field.Code
. Index字段的特殊之处在于它仅返回
Field.Code
。 This is the text within the field { brackets }. 这是{括号}字段中的文本。 So returning this
Range
won't put the focus outside the field, but you can get there... 因此,返回此
Range
不会将焦点放在视野之外,但是您可以到达那里...
First "collapse" the Range to its end-point (think of it like pressing Right arrow for a selection). 首先将范围“折叠”到其端点(可以像按向右箭头进行选择一样进行思考)。 Then move the starting point of the Range one character towards the end of the document - now it's outside the field.
然后将Range的起始点向文档末尾移动一个字符-现在它位于字段之外。
Word.Range rngField = null;
object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
object oMoveCharacter = Word.WdUnits.wdCharacter;
object oOne = 1;
foreach( Field f in document.Fields)
{
if( f.Type == WdFieldType.wdFieldIndexEntry)
{
rngField = f.Code;
rngField.Collapse(ref oCollapseEnd);
rngField.MoveStart(ref oMoveCharacter, ref oOne);
rngField.InsertAfter("{{Some After text}}"); }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.