Highlight a text in word document in c# ignoring c

2020-05-04 20:17发布

问题:

I am able to search/highlight a particular word in a word document with my code. But below is the problem I am facing.

If the search word is "it" then it searches for "it" But not "It"(case sensitive). I want to search both "it" and "It" ignoring cases. How do i solve this issue?

Below is the code

  private int FindLoop(Word._Application wordApp, object text,
                    Word._Document aDoc,
                    object aComment, out List<string> OccuranceList,
                    bool insertComment)
 {

int intFound = 0;
//object start = 0;
//object end = 1;
object missing = System.Reflection.Missing.Value;

object myfile = saveFileDialog.FileName;

Word.Range rng = wordApp.ActiveDocument.Range(ref missing, ref missing);




object readOnly = true;
//object isVisible = true;
object isVisible = false;
object oMissing = System.Reflection.Missing.Value;
string fname = textBox1.Text;


object matchWholeWord = true;

    object[] Parameters;
    OccuranceList = new List<string>();
    Parameters = new object[15];
    Parameters[0] = String.Format("<{0}>", text);
    Parameters[1] = true;
    Parameters[2] = missing;
    Parameters[3] = true;
    Parameters[4] = missing;
    Parameters[5] = missing;
    Parameters[6] = missing;
    Parameters[7] = missing;
    Parameters[8] = missing;
    Parameters[9] = text;
    Parameters[10] = missing;
    Parameters[11] = missing;
    Parameters[12] = missing;
    Parameters[13] = missing;
    Parameters[14] = missing;
    bool found = false;        {
    try
    {

        found = (bool)rng.Find.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, rng.Find, Parameters);





    }

    catch (Exception ex)
    {
        MessageBox.Show("Find Loop", ex.Message);
    }





    //while (rng.Find.Found)
    while (found)
    {



        intFound++;
        if (checkBox1.Checked == true)
        {
            if (fname.ToString().EndsWith("doc") || fname.ToString().EndsWith("docx"))
            {
                try
                {
                    if (rng.Text.Trim() == text.ToString())
                    {



                        // Add a new document 
                        aDoc = wordApp.Documents.Open(fname, ref oMissing,
                                                       ref readOnly, ref oMissing, ref oMissing, ref oMissing,
                                                       ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                                       ref oMissing, ref isVisible, ref oMissing, ref oMissing,
                                                       ref oMissing, ref oMissing);



                        rng.Font.Bold = 1;
                        rng.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;
                    }

                }

                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }

            }


        }
    }
} 

回答1:

Looking at the MSDN description of the find method, it looks like 'MatchCase' is the second parameter. So try:

Parameters[1] = false;

instead of

Parameters[1] = true;

Incidentally, your method of invoking Execute on the rng.Find object is a bit odd - why not just make the call to the Execute method directly? I think things will become a bit easier then.

I'm not a MS Word programmer (more Excel) but hopefully this will work. Try something like:

rng.Find.Execute(FindText: String.Format("<{0}>", text), MatchCase: false);

Add as many parameters as you require.

EDIT: The following code opens a document, finds "it" (upper or lower case) and highlights it. Tested and "works on my machine" :-)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Office.Interop.Word;

namespace Word
{
    class Program
    {
        static void Main(string[] args)
        {
            var fileName = @"C:\Scratch\test.docx";

            var app = new Application();
            app.Visible = true;

            var doc = app.Documents.Open(fileName);

            var rng = doc.Range();

            // This is the bit relevant to the question
            rng.Find.Text = "it";
            rng.Find.MatchCase = false;

            while (rng.Find.Execute(Forward: true))
            {
                rng.Font.Bold = 1;
                rng.HighlightColorIndex = WdColorIndex.wdDarkRed;
            }
        }
    }
}