简体   繁体   English

C#互操作:如何确定字段的范围?

[英]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.

相关问题 如何合并 Excel 范围 Interop C# - How to merge a Excel range Interop C# 使用C#Excel Interop选择范围 - select range using c# excel interop C#:Microsoft.Office.Interop.Excel.Range的ID字段不会与Excel表一起保留 - C#: ID-field of Microsoft.Office.Interop.Excel.Range is not persisted with an Excel-sheet 如何在C#Word Interop中指定范围之前确定行范围 - How to identify the range of lines before the specified range in c# word interop 如何确定在C#中使用Word Office Interop打开的活动文档的字体颜色? - How to determine the font color of the active document opened using word office interop in c#? 如何确定日期是否在C#中的日期范围内? - How can I determine if a date is within a range of dates in C#? C# Excel 互操作 - 如何检查范围内的单个单元格是否具有单元格边界? - C# Excel Interop — How to check if a single cell in a range has cell borders? 如何使用Word interop和C#从文档文件中获取特定文本的范围 - How to get the range of particular text from a doc file using Word interop and C# 如何使用c#Excel.Interop从Excel工作表中获取现有范围名称? - How to get an existing range name from an Excel worksheet using c# Excel.Interop? 如何在C#中使用互操作将现有的所有Excel范围向下移动一行 - how to move entire existing excel range to one row down using interop in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM