0

假设我必须在 MS-Word 文档中搜索多个字符串。我想将多个关键字传递给 word api,并且我需要该 api 将通过 MS-word 打开该 doc 或 docx 文件,如果在我提供的 ms-word 文件中找到这些词,则突出显示这些词。在这里,我得到了一个用于在 ms-word 文件中突出显示单词的示例代码,但我发现的例程可能不会突出显示多个单词。另一个问题我注意到,当它突出显示文件并打开时,它工作正常,但是当我关闭 ms-word 时,它要求保存更改。我了解此例程会修改文档以制作我不想要的突出显示。我希望该例程将突出显示但不会修改 doc 文件....有什么办法可以做到这一点。请指导。谢谢

using Word = Microsoft.Office.Interop.Word;

private void btnFind_Click(object sender, EventArgs e)
{
object fileName = "audi.doc"; //The filepath goes here
string textToFind = "test1,test2,test3"; //The text to find goes here
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object missing = System.Type.Missing;
try
{
    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);
    doc.Activate();
    foreach (Word.Range docRange in doc.Words)
    {
        if(docRange.Text.Trim().Equals(textToFind,
           StringComparison.CurrentCultureIgnoreCase))
        {
            docRange.HighlightColorIndex = 
              Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow;
            docRange.Font.ColorIndex = 
              Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
        }
    }
    System.Diagnostics.Process.Start(fileName.ToString());

}
catch (Exception ex)
{
    MessageBox.Show("Error : " + ex.Message);
}
}
4

1 回答 1

0

您可以使用Range.Find 属性

下面的代码以只读方式打开文档并选择您要查找的单词的所有匹配项,而无需更改文档。

这里有一些代码给你:

 private static void HighlightText()
    {
        object fileName = "C:\\1.doc";
        object textToFind = "test1";
        object readOnly = true;
        Word.Application word = new Word.Application();
        Word.Document doc = new Word.Document();
        object missing = Type.Missing;
        try
        {
            doc = word.Documents.Open(ref fileName, ref missing, ref readOnly,
                                      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);
            doc.Activate();


            object matchPhrase = false;
            object matchCase = false;
            object matchPrefix = false;
            object matchSuffix = false;
            object matchWholeWord = false;
            object matchWildcards = false;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object matchByte = false;
            object ignoreSpace = false;
            object ignorePunct = false;

            object highlightedColor = Word.WdColor.wdColorGreen;
            object textColor = Word.WdColor.wdColorLightOrange;

            Word.Range range = doc.Range();

            bool highlighted = range.Find.HitHighlight(textToFind,
                                                       highlightedColor,
                                                       textColor,
                                                       matchCase,
                                                       matchWholeWord,
                                                       matchPrefix,
                                                       matchSuffix,
                                                       matchPhrase,
                                                       matchWildcards,
                                                       matchSoundsLike,
                                                       matchAllWordForms,
                                                       matchByte,
                                                       false,
                                                       false,
                                                       false,
                                                       false,
                                                       false,
                                                       ignoreSpace,
                                                       ignorePunct,
                                                       false);

            System.Diagnostics.Process.Start(fileName.ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error : " + ex.Message);
            Console.ReadKey(true);
        }
    }
于 2013-03-06T13:14:38.190 回答