查找多个字符串&安培;突出显示在MS-Word文档的字符串 [英] Find multiple string & Highlight those string in a MS-Word Document

查看:177
本文介绍了查找多个字符串&安培;突出显示在MS-Word文档的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有搜索在MS-Word文档多个字符串。我想通过多个关键字词API和我需要的API将通过打开MS-词,DOC或DOCX文件,并强调这些字如果由我提供的MS-Word文件中。在这里,我得到了在MS-Word文件为亮点的话一个示例代码,但我发现例程可以不突出多字。另一个问题,我注意到,当它突出显示文件,然后打开它工作正常,但是当我关闭MS-字则要求保存更改。据我所知,这个程序修改文档制作,我不想highligh。我想,程序将突出,但不会修改doc文件....有没有办法做到这一点。请指导。谢谢



 使用Word =的Microsoft.Office.Interop.Word; 

私人无效btnFind_Click(对象发件人,EventArgs五)
{
目标文件名=audi.doc; //文件路径放在这里
字符串的textToFind =TEST1,TEST2,TEST3 //查找的文本放在这里
Word.Application字=新Word.Application();
Word.Document DOC =新Word.Document();
对象缺少= System.Type.Missing;

{$ B $(DOC)B = word.Documents.Open(REF文件名,参考失踪,失踪参考,
参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,
参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,
参考失踪,失踪参考,参考失踪);
doc.Activate();
的foreach(Word.Range docRange在doc.Words)
{
如果(docRange.Text.Trim()。等于(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());

}
赶上(异常前)
{
MessageBox.Show(错误:+ ex.Message);
}
}


解决方案

您可以使用 Range.Find地产



代码下面打开的文档为只读,选择你想找到不改变文档的单词的所有比赛。



下面为大家介绍一些代码:

 私有静态无效HighlightText()
{$ b $对象b文件名=C:\\1.doc
对象的textToFind =测试1;
对象readOnly的= TRUE;
Word.Application字=新Word.Application();
Word.Document DOC =新Word.Document();
对象缺少= Type.Missing;

{$ B $(DOC)B = word.Documents.Open(REF文件名,缺少文献,文献只读,已
参考失踪,失踪参考,参考失踪,
裁判失踪,楼盘失踪,失踪参考,
参考失踪,失踪参考,参考失踪,
参考失踪,失踪参考,参考失踪,
REF丢失);
doc.Activate();


对象matchPhrase = FALSE;
对象matchCase = FALSE;
对象matchPrefix = FALSE;
对象matchSuffix = FALSE;
对象matchWholeWord = FALSE;
对象matchWildcards = FALSE;
对象matchSoundsLike = FALSE;
对象matchAllWordForms = FALSE;
对象matchByte = FALSE;
对象ignoreSpace = FALSE;
对象ignorePunct = FALSE;

对象highlightedColor = Word.WdColor.wdColorGreen;
对象文字颜色= Word.WdColor.wdColorLightOrange;

Word.Range范围= doc.Range();

布尔强调= range.Find.HitHighlight(textToFind中,
highlightedColor,
文字颜色,
matchCase,
matchWholeWord,
matchPrefix,
matchSuffix,
matchPhrase,
matchWildcards,
matchSoundsLike,
matchAllWordForms,
matchByte,
假,
假,
错,
假,
假,
ignoreSpace,
ignorePunct,
假);

System.Diagnostics.Process.Start(fileName.ToString());
}
赶上(异常前)
{
Console.WriteLine(错误:+ ex.Message);
Console.ReadKey(真);
}
}


suppose i have to search multiple string in MS-Word document. i want to pass multiple keyword to word api and i need that api will open that doc or docx file by MS-word and highlight those word if found in ms-word file supplied by me. here i got a sample code for highlight words in ms-word file but the routine i found may not highlight multiple word. another problem i notice that when it highlight the file and open then it works fine but when i close the ms-word then it asking for saving changes. i understand that this routine modify the document for making the highligh which i do not want. i want that routine will highlight but will not modify the doc file....is there any way to do it. please guide. thanks

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

解决方案

You can use Range.Find Property.

Code below open document for read only and select all matches for the word you want to find without changing the document.

Here are some code for you:

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

这篇关于查找多个字符串&安培;突出显示在MS-Word文档的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆