在c#中突出显示word文档中的文本

时间:2013-10-10 07:17:12

标签: c# ms-word highlight

我可以使用我的代码搜索/突出显示单词文档中的特定单词。但下面是我面临的问题。

如果搜索词是“it”,那么它搜索“it”并且也搜索w“it”nessed。我只想搜索“它”这个词。我该如何解决这个问题?

 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;

        OccuranceList = new List<string>();
        object[] Parameters;
        Parameters = new object[15];
        Parameters[0] = text;
        Parameters[1] = missing;
        Parameters[2] = missing;
        Parameters[3] = missing;
        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 个答案:

答案 0 :(得分:1)

您需要为搜索的文本添加一些通配符。在您的情况下,要查找的文字将是:<it>而不是it。参考你的代码应该是这样的:

Parameters[0] = String.Format("<{0}>"; text);

此外,您需要将通配符参数设置为true:

Parameters[3] = true;

有关详细信息,请查看this link