How to Find and Highlight Specified Text in Word with C#, VB.NET

In Word, find function is used to find a specified word or sentence from document with lots of contents quickly. In this post, I want to introduce a method about how to find text and highlight it with color by using C#/VB.NET.

Actually, find function is often used by us. We often find many labels at the end of the document to explain some words. If we want to know where the words are, we can use find function to get it. Also, we can use find function to get key words or notes and set colors or special effect to tell readers what they may need to pay attention.

In my example, I prepare a document and I will find word “Breathless” from it and add background color to highlight all “Breathless”.

*The .NET Word component, Spire.Doc is used for realizing this function.

Detailed Step Shown as Following:

  1. Load the document which I want to find specified word.
  2. Find word. Declare a TextSelection array which saves word I will find and assign value as document.FindAllString method. I need to send three parameters to this method, word I want to find, bool value for case sensitive, bool value for whole word applied.
  3. Highlight word. Because there are several same words in a document, I use foreach sentence to highlight all words with specified  color in TextSelection array.
  4. Save and launch file.

Main Coding:

C#

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace FindWord
{
    class Find
    {
        static void Main(string[] args)
        {

            //Load Document
            Document document = new Document();
            document.LoadFromFile(@”D:\work\My Documents\Shayne Breathless.docx”, FileFormat.Docx);

            //Find Word
            TextSelection[] textSelections = document.FindAllString(“Breathless”, true, true);

            //Highlight Word
            foreach(TextSelection selection in textSelections)
            {
                selection.GetAsOneRange().CharacterFormat.HighlightColor = Color.Cyan;
            }

            //Save and Launch
            document.SaveToFile(“Find.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“Find.docx”);
        }
    }
}

VB.NET

Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace FindWord
    Friend Class Find
        Shared Sub Main(ByVal args() As String)

            ‘Load Document
            Dim document As New Document()
            document.LoadFromFile(“D:\work\My Documents\Shayne Breathless.docx”, FileFormat.Docx)

            ‘Find Word
            Dim textSelections() As TextSelection = document.FindAllString(“Breathless”, True, True)

            ‘Highlight Word
            For Each selection As TextSelection In textSelections
                selection.GetAsOneRange().CharacterFormat.HighlightColor = Color.Cyan
            Next selection

            ‘Save and Launch
            document.SaveToFile(“Find.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Find.docx”)
        End Sub
    End Class
End Namespace

Result Shown as Following:

Download Spire.Doc for .NET Here

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

2 thoughts on “How to Find and Highlight Specified Text in Word with C#, VB.NET”

  1. A motivating discussion is worth comment. There’s no doubt that that you need to publish more on this issue, it may not be a taboo subject but typically people do not talk about such issues.
    To the next! Kind regards!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s