Find and replace with highlighted text

Category: word for developers


ricky_CSharp on Tue, 10 May 2016 16:37:08


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



macropod on Tue, 10 May 2016 21:37:29

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
      .Text = "ERROR"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchCase = True
      .MatchWholeWord = True
    End With
    Do While .Find.Found
      If .InRange(Rng) = False Then Exit Sub
      If .Cells(1).ColumnIndex = 2 Then .HighlightColorIndex = wdRed
      .Collapse wdCollapseEnd
  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.

Paul Edstein
[MS MVP - Word]

Deepak Saradkumar Panchal on Wed, 11 May 2016 02:11:20

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.



Shawn Sh on Wed, 11 May 2016 02:14:51

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

ricky_CSharp on Wed, 11 May 2016 09:29:31

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)



ricky_CSharp on Wed, 11 May 2016 09:31:53

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.


- Ricky

Deepak Saradkumar Panchal on Wed, 11 May 2016 09:56:05

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.



ricky_CSharp on Wed, 11 May 2016 12:24:10

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.HighlightColorIndex = Word.WdColorIndex.wdRed;
//This changes color of only first "ERROR"

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

ricky_CSharp on Wed, 11 May 2016 14:15:32'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


macropod on Wed, 11 May 2016 21:00:46

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.