none
Highlight text in Word Document C# .net

    Question

  • Here's what I need to do.
    I need to open up a Word Document and then highlight all words in it that match the words in a string arrary.
    I am referencing Microsoft.Office.Core, instantiating Application Class and a Document.
    I am kind of lost as to how to proceed further.

    Any and all help highly appreciated.  Thank you.
    Wednesday, September 09, 2009 8:40 PM

Answers

  • Following code snippet may give some help:

                object missObj = Missing.Value;
                object path = @"D:\mydoc.docx";
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref path, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj);
                foreach (Microsoft.Office.Interop.Word.Range range in doc.Words)
                {
                    if (range.Text.Trim() == "the text")
                    {
                        range.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow;
                        range.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
                    }
                }

    Thanks,
    Eric


    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    • Proposed as answer by eryang Monday, September 14, 2009 3:10 AM
    • Marked as answer by gtes Monday, September 14, 2009 5:33 PM
    Friday, September 11, 2009 6:00 AM

All replies

  • I have a blog entry that shows how to bold specific words in a Word document. That may help you get started:

    http://msmvps.com/blogs/deborahk/archive/2009/08/31/bolding-text-in-microsoft-word-using-net.aspx

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by gtes Thursday, September 10, 2009 2:38 PM
    • Unmarked as answer by gtes Thursday, September 10, 2009 3:29 PM
    Wednesday, September 09, 2009 9:00 PM
  • Following code snippet may give some help:

                object missObj = Missing.Value;
                object path = @"D:\mydoc.docx";
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref path, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj, ref missObj);
                foreach (Microsoft.Office.Interop.Word.Range range in doc.Words)
                {
                    if (range.Text.Trim() == "the text")
                    {
                        range.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow;
                        range.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
                    }
                }

    Thanks,
    Eric


    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    • Proposed as answer by eryang Monday, September 14, 2009 3:10 AM
    • Marked as answer by gtes Monday, September 14, 2009 5:33 PM
    Friday, September 11, 2009 6:00 AM
  • Thank you Eric.  That's what I needed.
    Highly appreciate your help.
    Monday, September 14, 2009 2:34 PM
  • Hi all,

    in my word Document is having text [[Spell]].if i loop through the words i am gettiing only the [[ as one word and Spell is one word and ]] as one word. i want to replace entire string with my image.is it possible?
    please help on this ...

    code here :Find the To Do part

    foreach

     

    (Microsoft.Office.Interop.Word.Range range in objDocument.Words)

    {

     

    if (range.Find.Text == "[[Spell]]" ) //TO DO:Need some condition to identify full text here along with [[]]

    {

     

    //TODO : Ineed to replace and insert my image ???
    object
    oRng2 = range;

    objDocument.InlineShapes.AddPicture(signaturePath,

    ref missing, ref missing, ref oRng2);

    found =

    true;

    }

     

    }

    Tuesday, December 22, 2009 9:57 AM
  • Hi eryang,

    in my word Document is having text [[Spell]].if i loop through the words i am gettiing only the [[ as one word and Spell is one word and ]] as one word. i want to replace entire string with my image.is it possible?
    please help on this ...

    code here :Find the To Do part

    foreach

     

    (Microsoft.Office.Interop.Word.Range range in objDocument.Words)

     

    if (range.Find.Text == "[[Spell]]" ) //TO DO:Need some condition to identify full text here along with [[]]

     

    //TODO : Ineed to replace and insert my image ???
    object
    oRng2 = range;

    objDocument.InlineShapes.AddPicture(signaturePath,

     

    ref missing, ref missing, ref oRng2);

    found =

     

    true;

    }

     

    }

    Thanks
    Vruk

    Tuesday, December 22, 2009 9:58 AM
  • Hello
    I'm saving word documents and even pdf document in a table of a SQL 2005 database
    the database is fore-ended by a windows application in which the user may type a keyword and then using FTS I can search for the word inside the word document (which is stored in a varbinary(max) column)

    what I need is to display the resulting documents as readonly in browser (it's a control that can be put on the win form) highliting the hits in yellow for example
    you reply sounds interesting but is there a general way to highlight hits in any type of document (.doc, .xsl, .pdf,...)

    Do you have any idea for doing that?
    thank you
    Saturday, March 13, 2010 5:22 AM
  • I suggest to use a c# word component Spire.Doc, I use it long time, quit good.
    It supports search a word in a string and highlight the word .
    May help to you.
    Tuesday, August 10, 2010 4:52 AM