Find multiple string & Highlight those string in a

2019-06-14 10:21发布

问题:

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);
}
}

回答1:

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);
        }
    }