C#:在Word中搜索文本并获取结果的范围

时间:2010-10-25 16:55:31

标签: c# ms-word find

我可以通过以下方式在Word文件中找到文本:

Word.Range range = wordApp.ActiveDocument.Content;
Word.Find find = range.Find;
find.Text = "xxx";
find.ClearFormatting();
find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing);

这告诉我是否找到了文本。但是我需要找到文本片段的范围。

5 个答案:

答案 0 :(得分:8)

你试过这个:

 range.Find.Execute(
      ref missing, ref missing, ref missing, ref missing, ref missing, 
      ref missing, ref missing, ref missing, ref missing, ref missing,
      ref missing, ref missing, ref missing, ref missing, ref missing);


 while (range.Find.Found) 
{ 
   //Get selected index.
   // Do as you please with range...
   //Positions:  range.Start... range.End
   //search again
   range.Find.Execute(
      ref missing, ref missing, ref missing, ref missing, ref missing, 
      ref missing, ref missing, ref missing, ref missing, ref missing,
      ref missing, ref missing, ref missing, ref missing, ref missing);
} 

答案 1 :(得分:3)

应该通过执行find来更改范围对象。

因此,您可能会使用range.Startrange.End来获取角色位置。 Reference

答案 2 :(得分:2)

使用find方法从word获取范围并对其进行格式化。

//Parameter contains what you want to find.
_wordApp.Selection.Find.Execute(title); 

Word.Range range = _wordApp.Selection.Range;
if (range.Text.Contains(title))
{
     //gets desired range here it gets last character to make superscript in range 
     Word.Range temprange = _document.Range(range.End - 1, range.End);          
     temprange.Select();
     Word.Selection currentSelection = _wordApp.Selection;
     currentSelection.Font.Superscript = 1;
}

答案 3 :(得分:2)

range.Find.Execute如果找到则返回true,并将range设置为找到的范围:

var range = doc.Range();
while ( range.Find.Execute("xxx") ) 
    Debug.Print( range.Text );

请注意,range.Find.Execute会在range之后搜索范围,如果range已经与查找条件匹配(在第一个range.Find.Execute之后)。

例如,此VBA宏只会找到第二个" b" :

Sub Macro1()
    ActiveDocument.Range.Text = "abba"
    Dim r As Range
    Set r = ActiveDocument.Range(1, 2) ' the first "b"
    Debug.Print r.Start; r.End      ' prints " 1  2 "

    Debug.Print r.Find.Execute("b") ' prints "True"
    Debug.Print r.Start; r.End      ' prints " 2  3 "

    Debug.Print r.Find.Execute("b") ' prints "False" (if r.Find.Wrap = wdFindStop)
    Debug.Print r.Start; r.End      ' prints " 2  3 "
End Sub

答案 4 :(得分:0)

可以这样解决:

    /// <summary>
    /// Find all
    /// </summary>
    /// <param name="range"></param>
    /// <param name="findText">Find content</param>
    public static List<Range> FindAll(this Range range, string findText)
    {
        int start = range.Start;
        int end = range.End;

        List<Range> ranges = new List<Range>();

        range.Find.Execute(FindText: findText, MatchCase: true);
        while (range.Find.Found)
        {
            //The search will change the range, here is an out-of-range judgment
            if (range.Start > end)
                break;

            ranges.Add(range.Document.Range(range.Start, range.End));
            range.Find.Execute(FindText: findText, MatchCase: true);
        }

        //Restore the original range
        range.SetRange(start, end);
        return ranges;
    }

也可以创建扩展方法:

        /// <summary>
    /// 查找第一个
    /// </summary>
    /// <param name="range"></param>
    /// <param name="findText">查找内容</param>
    /// <returns>没有找到为空</returns>
    public static Range FindFirst(this Range range, string findText)
    {
        int start = range.Start;
        int end = range.End;

        bool isOk = range.Find.Execute(FindText: findText, MatchCase: true);
        if (isOk)
        {
            var newRange = range.Document.Range(range.Start, range.End);
            range.SetRange(start, end);
            return newRange;
        }
        else
            return null;
    }