0

コードを使用して、Word ドキュメント内の特定の単語を検索/強調表示できます。しかし、以下は私が直面している問題です。

検索語が "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);
                        }

                    }


                }
     }
  }
4

1 に答える 1

1

検索するテキストにワイルドカードを追加する必要があります。あなたの状況では、検索するテキストは次のようになります:<it>の代わりにit. コードを参照すると、次のようになります。

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

さらに、ワイルドカード パラメータを true に設定する必要があります。

Parameters[3] = true;

詳細については、このリンクを確認してください。

于 2013-10-10T07:36:33.313 に答える