在word里查找所有匹配的单词并修改文字颜色
自己开发一个程序,其中有一个功能是在word里查找所有匹配的单词并修改文字颜色,我用的是Selection。
遇到的问题是,只能修改第一个匹配到的单词颜色,而不是所有的。
代码如下:
object oMissingValue = Type.Missing;
object oFileName = sthFileName;
Word.Application appWord = new Word.Application(); //打开word程序
Word.Selection curSelect = appWord.Selection; //实例化一个selection接口
curSelect.Find.ClearFormatting();
curSelect.Find.Text = sthKeys; //查询的文字
//查找对象
curSelect.Find.Execute(ref oMissingValue, ref oMissingValue,
true, ref oMissingValue,
ref oMissingValue, true,
ref oMissingValue, ref oMissingValue,
ref oMissingValue, ref oMissingValue,
ref oMissingValue, ref oMissingValue,
ref oMissingValue, ref oMissingValue,
ref oMissingValue);
string oth = curSelect.Text;
curSelect.Font.Color = Word.WdColor.wdColorRed;//设置颜色为红
//清空Range对象
//Microsoft.Office.Interop.Word.Range range = null;
//保存
docWord.Save();
docWord.Close(ref oMissingValue, ref oMissingValue, ref oMissingValue);
appWord.Quit(ref oMissingValue, ref oMissingValue, ref oMissingValue);