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

发布于 2024-09-28 23:00:20 字数 434 浏览 1 评论 0原文

我可以通过以下方式在 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);

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

I can find a text in a Word file via:

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);

This tells me if the text is found. But I need the range of the found text-piece.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

违心° 2024-10-05 23:00:20

你有没有尝试过这个:

 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);
} 

Have you tried this:

 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);
} 
扛刀软妹 2024-10-05 23:00:20

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

因此,您可能会使用 range.Startrange.End 来获取字符位置。
参考

The range object should be changed by executing find on it.

So, likely you'd use range.Start and range.End to get the character positions.
Reference

对你而言 2024-10-05 23:00:20

使用 find 方法获取单词的范围并格式化它。

//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;
}

Gets a range from word using find method and format it.

//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;
}
牛↙奶布丁 2024-10-05 23:00:20

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

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

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

例如,此 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

range.Find.Execute returns true if found, and sets range to the found range:

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

Note that range.Find.Execute will search the range after range if range is already a match for the Find conditions (after the first range.Find.Execute).

For example, this VBA Macro will find only the second "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
携君以终年 2024-10-05 23:00:20

可以这样解决:

    /// <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;
    }

Can be solved like this:

    /// <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;
    }

You can also create an extension method:

        /// <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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文