How to highlight all occurrences of a word in a Word document??
-
Hi I want to highlight or more like select all the occurrences of a word. But I have googled a lot and haven't found anything about that. When I have tried, it just selects one appearance of the word, the last.
Word.Application ap = new Word.Application();
doc = ap.Documents.Open(path, ReadOnly: false, Visible: true);
ap.Selection.Find.ClearFormatting();
ap.Selection.Find.Text = txtFind.Text;
ap.Selection.Find.Execute();
while (ap.Selection.Find.Found)
{
ap.Selection.Select();
ap.Selection.Find.Execute();
}
doc.Activate();
ap.Visible = true;
ap.ActiveWindow.SetFocus();I have tried so many different approaches, but haven't been able to solve it. I don't want to highlight the words, I just want them to be selected as they are when you choose find all in Microft Word. Hope somebody can help me. Many thanks Fia
-
Hi I want to highlight or more like select all the occurrences of a word. But I have googled a lot and haven't found anything about that. When I have tried, it just selects one appearance of the word, the last.
Word.Application ap = new Word.Application();
doc = ap.Documents.Open(path, ReadOnly: false, Visible: true);
ap.Selection.Find.ClearFormatting();
ap.Selection.Find.Text = txtFind.Text;
ap.Selection.Find.Execute();
while (ap.Selection.Find.Found)
{
ap.Selection.Select();
ap.Selection.Find.Execute();
}
doc.Activate();
ap.Visible = true;
ap.ActiveWindow.SetFocus();I have tried so many different approaches, but haven't been able to solve it. I don't want to highlight the words, I just want them to be selected as they are when you choose find all in Microft Word. Hope somebody can help me. Many thanks Fia
You are missing some properties for your Selection.Find list. Try setting
Forward = true
andWrap = wdFindContinue
. To find these values, you can use the macro recorder in Word and perform a Find all operation - this should give you a good place to start.Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
You are missing some properties for your Selection.Find list. Try setting
Forward = true
andWrap = wdFindContinue
. To find these values, you can use the macro recorder in Word and perform a Find all operation - this should give you a good place to start.Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Hi Thanks for your reply, but I have already tried that, but nothing changes. It just selects the last occurrence of the word. I really don't want to use a macro. Aren't there another way to accomplish this. Many thanks Fia
Use the macro to tell you what options to set, not to actually run the find.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Use the macro to tell you what options to set, not to actually run the find.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Hi I want to highlight or more like select all the occurrences of a word. But I have googled a lot and haven't found anything about that. When I have tried, it just selects one appearance of the word, the last.
Word.Application ap = new Word.Application();
doc = ap.Documents.Open(path, ReadOnly: false, Visible: true);
ap.Selection.Find.ClearFormatting();
ap.Selection.Find.Text = txtFind.Text;
ap.Selection.Find.Execute();
while (ap.Selection.Find.Found)
{
ap.Selection.Select();
ap.Selection.Find.Execute();
}
doc.Activate();
ap.Visible = true;
ap.ActiveWindow.SetFocus();I have tried so many different approaches, but haven't been able to solve it. I don't want to highlight the words, I just want them to be selected as they are when you choose find all in Microft Word. Hope somebody can help me. Many thanks Fia
-
Repeating what was already said.... 1. Create a macro that does what you want. 2. LOOK at the macro. Figure out what it is doing. 3. Use the information that you LEARNed in 2 to create your C# code.
-
Hi Thanks for your reply, but I have already tried that, but nothing changes. It just selects the last occurrence of the word. I really don't want to use a macro. Aren't there another way to accomplish this. Many thanks Fia