0

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

如果搜索词是“it”,那么它会搜索“it”而不是“It”(区分大小写)。我想同时搜索“it”和“It”而忽略大小写。我该如何解决这个问题?

下面是代码

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

            }


        }
    }
} 
4

1 回答 1

1

查看find 方法的 MSDN 描述,看起来 'MatchCase' 是第二个参数。所以试试:

Parameters[1] = false;

代替

Parameters[1] = true;

顺便说一句,您在 rng.Find 对象上调用 Execute 的方法有点奇怪 - 为什么不直接调用 Execute 方法呢?我认为那时事情会变得容易一些。

我不是 MS Word 程序员(更多 Excel),但希望这能奏效。尝试类似:

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

根据需要添加尽可能多的参数。

编辑:以下代码打开一个文档,找到“它”(大写或小写)并突出显示它。经过测试并“在我的机器上工作”:-)

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;
            }
        }
    }
}
于 2013-10-17T09:24:03.817 回答