Robin
Robin

Reputation: 87

Highlight text ignoring cases in word document in c#

With the below code am able to highlight search text. But if the search text is "also" then "Also" is not getting highlighted. How to ignore cases.

 foreach (Word.Range w in doc.Words)
                            {

                                for (int i = 1; i < xmlnode.Count; i++)
                                {

                                    XmlAttributeCollection xmlattrc = xmlnode[i].Attributes;
                                    object text = xmlnode[i].FirstChild.InnerText;

                                    if (w.Text.Trim() == text.ToString())
                                    {
                                        w.Font.Bold = 1;
                                        w.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow;
                                    }
                                }
                            }
                        }

Upvotes: 0

Views: 362

Answers (2)

user3306420
user3306420

Reputation: 11

       // int counter = 0;
        object readOnly = true;
        System.IO.StreamReader file =
         new System.IO.StreamReader(@"C:\Users\noor\Desktop\HIGH LIGHTER\Sample.txt");
        object matchWholeWord = true;
        //string textToFind  = "to";
        //string my = System.IO.File.ReadAllText(@"C:\Users\noor\Desktop\HIGH LIGHTER\Sample.txt");
        //var textToFind = my ;
       // textBox1.Text = textToFind.ToString ();
        while ((line = file.ReadLine()) != null)
        {

            // Define an object to pass to the API for missing parameters
            object missing = System.Type.Missing;
            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);
            string ReadValue = string.Empty;
            // Activate the document
            doc.Activate();

            /* foreach (Word.Range tmpRange in doc.StoryRanges)
             {
                 ReadValue += tmpRange.Text;
             }
         */
            foreach (Word.Range docRange in doc.Words)
                try
                {

                    if (docRange.Text.Trim().Equals(line, StringComparison.CurrentCulture) == false )
                    {
                        docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow;
                        docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error : " + ex.Message);
                }
        }
           // counter++;


        file.Close();

    }

Upvotes: 1

o_weisman
o_weisman

Reputation: 604

You could probably use the string Compare method:

if(String.Compare(w.Text.Trim(), text.ToString(), true) == 0)

Upvotes: 0

Related Questions