none
Find and replace with highlighted text RRS feed

  • Question

  • Hi,


    I have a table and want to find a text in specific column and highlight the found text.

    For example: In column 2 of my table, all words "ERROR" has to be selected and has to be replaced with <mark>ERROR</mark> (highlighted in yellow permanently).

    I am able to find text and replace it with another text, but unable to highlight it "permanently". Please don't confuse it with hitHighlight.

    Till now, I am doing it by going through each cell of my column, but it is too much time consuming.

    if (!string.IsNullOrEmpty(t.Cell(rowCounter, 2).Range.Text))
    {                    
       if (str.Equals("ERROR"))
           t.Cell(rowCounter, 2).Range.HighlightColorIndex = Word.WdColorIndex.wdRed;
    }

    Can someone please help?

    Thanks and greeting

    Ricky



    • Edited by ricky_CSharp Tuesday, May 10, 2016 4:41 PM Highlight text
    Tuesday, May 10, 2016 4:37 PM

Answers

  • Ok..it's done with a way out.

    _application.Options.DefaultHighlightColorIndex = Word.WdColorIndex.wdRed;
    //change default highlight color
    object findText = "ERROR";
    object replaceText = System.Reflection.Missing.Value;
    object replaceAll = Word.WdReplace.wdReplaceAll;
    
    Word.Range rng = _table.Range;            
    
    rng.Find.Replacement.Highlight = 1;
    
    rng.Find.Execute(ref findText, ref FALSE, ref MISSING, ref MISSING, ref MISSING, ref MISSING, ref MISSING, ref MISSING, ref MISSING, ref replaceText, ref replaceAll, ref MISSING, ref MISSING, ref MISSING, ref MISSING);


    Thanks ror inputs

    -Ricky

    • Edited by ricky_CSharp Wednesday, May 11, 2016 2:16 PM
    • Marked as answer by ricky_CSharp Wednesday, May 11, 2016 2:16 PM
    Wednesday, May 11, 2016 2:15 PM

All replies

  • I'd be inclined to use Word's Find function. In VBA, that might be coded as:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range
    With ActiveDocument.Tables(1)
      Set Rng = .Range
      With .Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "ERROR"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchCase = True
          .MatchWholeWord = True
          .Execute
        End With
        Do While .Find.Found
          If .InRange(Rng) = False Then Exit Sub
          If .Cells(1).ColumnIndex = 2 Then .HighlightColorIndex = wdRed
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
    End With
    Application.ScreenUpdating = True
    End Sub

    I'll leave it to you to do the C# conversion.

    PS: Your post says you want to use Yellow highlight, but your code uses Red - my code does likewise.


    Cheers
    Paul Edstein
    [MS MVP - Word]



    • Edited by macropodMVP Tuesday, May 10, 2016 9:38 PM typo
    Tuesday, May 10, 2016 9:37 PM
  • Hi ricky_CSharp,

    As you are able to search and replace text in table. so I only give line of code that will highlight the text in table.

     cell.Range.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdYellow;

    you can save the document after that so it will Highlight permanently.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 11, 2016 2:11 AM
    Moderator
  • This article which demonstrates how to find, replace and highlight text in word might helps: How to find and replace Text on Word document programmatically

    Wednesday, May 11, 2016 2:14 AM
  • Hello Paul,

    the soulution works. But here again, I have to iterate through each "found" text and then highlight it. And for work docs which are 200 pages long, and has let's say 10 "ERROR" on each page, the iteration is time consuming.

    What I am looking for is similar function as "Replace All".

    For e.g. (symbolic code)

    rng.Fing.Execute(ref "ERROR", ..., ref "REPLACED_ERROR_TEXT", ref Word.WdReplace.wdReplaceAll..)

    Is there something that can find "ERROR" and highlight it with Yellow/Red Color (also called background color)

    Thanks

    Ricky

    Wednesday, May 11, 2016 9:29 AM
  • Hello Deepak,

    Thanks but still in the solution provided by you, I have to highloght each and every cell where I have found the text. This is something, I am able to do but as I said for long word docs it takes a lot of time which I want to avoid by something like highlight all.

    Regards

    - Ricky

    Wednesday, May 11, 2016 9:31 AM
  • Hi ricky_CSharp,

    I have something that I want to confirm first.

    do you want to highlight the text or highlight the cell of table?

    above you have mentioned that you want to do like replaceall but to do highlight you have to iterate.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 11, 2016 9:56 AM
    Moderator
  • With replace was just an example.Actually, I want to highlight all the matching text in a cell(s) of a table.

    My table has 1000 rows and column 2 may or may not contain "ERROR" word. If it contains word "ERROR" it should then be highlighted with red/yellow background color.

    I can do the above by iteration using Find.Found which is time consuming and do change color one after the another found text.

    Word.Range rng = _table.Range;
    rng.Find.Text = "ERROR";
    rng.Find.Execute();
    
    rng.HighlightColorIndex = Word.WdColorIndex.wdRed;
    //This changes color of only first "ERROR"

    How can I find all "ERROR" text and change color without iteration?

    Wednesday, May 11, 2016 12:24 PM
  • Ok..it's done with a way out.

    _application.Options.DefaultHighlightColorIndex = Word.WdColorIndex.wdRed;
    //change default highlight color
    object findText = "ERROR";
    object replaceText = System.Reflection.Missing.Value;
    object replaceAll = Word.WdReplace.wdReplaceAll;
    
    Word.Range rng = _table.Range;            
    
    rng.Find.Replacement.Highlight = 1;
    
    rng.Find.Execute(ref findText, ref FALSE, ref MISSING, ref MISSING, ref MISSING, ref MISSING, ref MISSING, ref MISSING, ref MISSING, ref replaceText, ref replaceAll, ref MISSING, ref MISSING, ref MISSING, ref MISSING);


    Thanks ror inputs

    -Ricky

    • Edited by ricky_CSharp Wednesday, May 11, 2016 2:16 PM
    • Marked as answer by ricky_CSharp Wednesday, May 11, 2016 2:16 PM
    Wednesday, May 11, 2016 2:15 PM
  • Your original post was quite specific about limiting the highlighting to column 2, which is why my code had that refinement. The code you posted ignores the question of which column the text is in and will highlight it in other columns as well, if found.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, May 11, 2016 9:00 PM