0

suppose i have to search multiple string in MS-Word document. i want to pass multiple keyword to word api and i need that api will open that doc or docx file by MS-word and highlight those word if found in ms-word file supplied by me. here i got a sample code for highlight words in ms-word file but the routine i found may not highlight multiple word. another problem i notice that when it highlight the file and open then it works fine but when i close the ms-word then it asking for saving changes. i understand that this routine modify the document for making the highligh which i do not want. i want that routine will highlight but will not modify the doc file....is there any way to do it. please guide. thanks

using Word = Microsoft.Office.Interop.Word;

private void btnFind_Click(object sender, EventArgs e)
{
object fileName = "audi.doc"; //The filepath goes here
string textToFind = "test1,test2,test3"; //The text to find goes here
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object missing = System.Type.Missing;
try
{
    doc = word.Documents.Open(ref fileName, ref missing, ref missing, 
    ref missing, ref missing, ref missing, ref missing, ref missing, 
    ref missing, ref missing, ref missing, ref missing, ref missing, 
    ref missing, ref missing, ref missing);
    doc.Activate();
    foreach (Word.Range docRange in doc.Words)
    {
        if(docRange.Text.Trim().Equals(textToFind,
           StringComparison.CurrentCultureIgnoreCase))
        {
            docRange.HighlightColorIndex = 
              Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow;
            docRange.Font.ColorIndex = 
              Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
        }
    }
    System.Diagnostics.Process.Start(fileName.ToString());

}
catch (Exception ex)
{
    MessageBox.Show("Error : " + ex.Message);
}
}
Thomas
  • 33,544
  • 126
  • 357
  • 626

1 Answers1

0

You can use Range.Find Property.

Code below open document for read only and select all matches for the word you want to find without changing the document.

Here are some code for you:

 private static void HighlightText()
    {
        object fileName = "C:\\1.doc";
        object textToFind = "test1";
        object readOnly = true;
        Word.Application word = new Word.Application();
        Word.Document doc = new Word.Document();
        object missing = Type.Missing;
        try
        {
            doc = word.Documents.Open(ref fileName, ref missing, ref readOnly,
                                      ref missing, ref missing, ref missing,
                                      ref missing, ref missing, ref missing, 
                                      ref missing, ref missing, ref missing, 
                                      ref missing, ref missing, ref missing, 
                                      ref missing);
            doc.Activate();


            object matchPhrase = false;
            object matchCase = false;
            object matchPrefix = false;
            object matchSuffix = false;
            object matchWholeWord = false;
            object matchWildcards = false;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object matchByte = false;
            object ignoreSpace = false;
            object ignorePunct = false;

            object highlightedColor = Word.WdColor.wdColorGreen;
            object textColor = Word.WdColor.wdColorLightOrange;

            Word.Range range = doc.Range();

            bool highlighted = range.Find.HitHighlight(textToFind,
                                                       highlightedColor,
                                                       textColor,
                                                       matchCase,
                                                       matchWholeWord,
                                                       matchPrefix,
                                                       matchSuffix,
                                                       matchPhrase,
                                                       matchWildcards,
                                                       matchSoundsLike,
                                                       matchAllWordForms,
                                                       matchByte,
                                                       false,
                                                       false,
                                                       false,
                                                       false,
                                                       false,
                                                       ignoreSpace,
                                                       ignorePunct,
                                                       false);

            System.Diagnostics.Process.Start(fileName.ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error : " + ex.Message);
            Console.ReadKey(true);
        }
    }
Chepene
  • 1,128
  • 1
  • 12
  • 18
  • thanks...your code looks promising. i have not tested this code. so just tell me this code will highlight word what i will supply to find out? this code will open the word doc in readonly mode ? if yes just tell me if user try to edit the word file after highlighting & opening this file then user can edit the doc file or not. please guide me. thanks – Thomas Mar 07 '13 at 07:38
  • Tomas, this code will highlight all inclusions of word in file. If you want to highlight only first inclusion, change parameters of `range.Find.HitHighlight()` function. Yes, code opens doc for readonly mode, user can edit the document. – Chepene Mar 07 '13 at 10:24
  • i am getting error when i run ur code. error occur at range.find.highlight() function and the error message is "Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE))" so please have a look at ur code and tell me what i need to change to fix the error. thanks – Thomas Mar 07 '13 at 13:10