none
Find and highlight a match in a range in word document RRS feed

  • Question

  • HI,

    is there any way to find a next match in a specified range if there are 2 matches in the range. Or if there is any way to get a list of all the matches. currently I am using the below interop method to find the match but it will highlight only the first match.

    bool found = foundRange.Find.Execute(ref oFindText, 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);

    Thanks.

    • Moved by CoolDadTx Monday, March 27, 2017 2:03 PM Office related
    Monday, March 27, 2017 6:22 AM

Answers

  • i want to highlight the matches in the document but i want to do it in c#.

    i have found the below solution for it:

     //Get any range you want
    var range = app.ActiveDocument.StoryRanges[WdStoryType.wdMainTextStory];
    
    
    var document = range.Document;
    
    //We want the variable range to continue refering to the same Range at all times
    var foundRange = range.Duplicate;
    
    if (foundRange.Find.Execute("j", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, WdFindWrap.wdFindStop, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)) {
        Console.WriteLine("Found first");
        //After using .Execute(), FoundRange has been set to the found text
        Console.WriteLine(foundRange.Text);
    }
    else
    {
        Console.WriteLine("Didn't find first");
    }
    
    //Set foundRange to start at the character after the last find and to end where the original range ends
    foundRange = document.Range(foundRange.Start + 1, range.End);
    
    //Repeat. Obviously you could use some kind of loop
    if (foundRange.Find.Execute("j", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, WdFindWrap.wdFindStop, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing))
    {
        Console.WriteLine("Found second");
        Console.WriteLine(foundRange.Text);
    }
    else
    {
        Console.WriteLine("Didn't find second");
    }
    
    foundRange.Select(); //Just to verify. We don't need the selection object for anything
    

    • Marked as answer by James Shokeen Tuesday, March 28, 2017 8:39 AM
    Tuesday, March 28, 2017 8:39 AM

All replies

  • What do you want to do with the matches once you have them? In VBA you could use code like to following to loop through all the matches in a document's body:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = InputBox("What is the Text to Find")
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        i = i + 1
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    MsgBox i & " instances found."
    End Sub

    To limit the Find scope to a nominated range (in this case, the first table in the document), you might use code like:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range, i As Long
    With ActiveDocument.Tables(1)
      Set Rng = .Range
      With .Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = InputBox("What is the Text to Find")
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
          .Execute
        End With
        Do While .Find.Found
          If .InRange(Rng) Then
            i = i + 1
          Else
            Exit Do
          End If
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
    End With
    Application.ScreenUpdating = True
    MsgBox i & " instances found."
    End Sub

    Both macros simply return a count of the matches made.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, March 28, 2017 12:21 AM
  • i want to highlight the matches in the document but i want to do it in c#.

    i have found the below solution for it:

     //Get any range you want
    var range = app.ActiveDocument.StoryRanges[WdStoryType.wdMainTextStory];
    
    
    var document = range.Document;
    
    //We want the variable range to continue refering to the same Range at all times
    var foundRange = range.Duplicate;
    
    if (foundRange.Find.Execute("j", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, WdFindWrap.wdFindStop, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)) {
        Console.WriteLine("Found first");
        //After using .Execute(), FoundRange has been set to the found text
        Console.WriteLine(foundRange.Text);
    }
    else
    {
        Console.WriteLine("Didn't find first");
    }
    
    //Set foundRange to start at the character after the last find and to end where the original range ends
    foundRange = document.Range(foundRange.Start + 1, range.End);
    
    //Repeat. Obviously you could use some kind of loop
    if (foundRange.Find.Execute("j", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, WdFindWrap.wdFindStop, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing))
    {
        Console.WriteLine("Found second");
        Console.WriteLine(foundRange.Text);
    }
    else
    {
        Console.WriteLine("Didn't find second");
    }
    
    foundRange.Select(); //Just to verify. We don't need the selection object for anything
    

    • Marked as answer by James Shokeen Tuesday, March 28, 2017 8:39 AM
    Tuesday, March 28, 2017 8:39 AM