[不是问题]在单词中高亮显示多个关键字 [英] [Not a question] Highklight multiple keywords in word

查看:80
本文介绍了[不是问题]在单词中高亮显示多个关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单词中实现了突出显示单个关键字

但现在想要实现多个关键字高亮





我的代码是

I have implemented highlight single keyword in word
But now want to implement multiple keywords highlight


My code is

object filename = openFileDialog1.FileName;
                string textToFind = "is"; //The text to find goes here
                Word.Application word = new Word.Application();
                Word.Document WordDoc = new Word.Document();
                object missing = System.Type.Missing;
                try
                {
                    WordDoc = 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);
                    WordDoc.Activate();
                    foreach (Word.Range docRange in WordDoc.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;
                        }
                    }
                    Microsoft.Office.Interop.Word.Application words = new Microsoft.Office.Interop.Word.Application();
                    WordDoc.Close(Word.WdSaveOptions.wdSaveChanges);



任何建议


Any suggestions

推荐答案

尝试:

Try:
//Change to List
list<string> textToFind = new list<string>();

foreach (Word.Range docRange in WordDoc.Words) {
     //add additional loop for each string
     foreach (string txt in textToFind) {
         if (docRange.Text.Trim().Equals(txt, StringComparison.CurrentCultureIgnoreCase)) ...
         //other stuff goes here
     }
}
</string></string>



这可能不是最好的方法,但它应该可以解决问题。

所有你需要做的就是填充textToFind。


This may not be the best way to do this but it should do the trick.
All you have to do is populate the textToFind.


这篇关于[不是问题]在单词中高亮显示多个关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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