首先说明的是,我不懂VSTO,而是直接引用的 MS Word 的 COM组件,但是对象模型好像跟VSTO差不多,嘿嘿。
首先看一下代码:
object oMissing = Type.Missing; object oFalse = false; object oTrue = true; Application app = new ApplicationClass(); app.Visible = false; object oFilename = "test.doc"; // 打开一个WORD文档 Document doc = app. Documents.Open(ref oFilename, ref oMissing, ref oFalse, ref oFalse, ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing); // 此属性为True则表示搜索文档格式或按文档格式搜索 doc.Content.Find.Format = true; doc.Content.Find.Forward = true; // 此属性表示我要搜索红色的文字 doc.Content.Find.Font.Color = WdColor.wdColorRed; doc.Content.Find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); System.Diagnostics.Trace.WriteLine(doc.Content.Find.Text); // 将搜索到的文字显示出来 doc.Content.Find.ClearFormatting(); doc.Save(); doc.Close(ref oMissing, ref oMissing, ref oMissing); app.Quit(ref oMissing, ref oMissing, ref oMissing);
关键的就app.ActiveDocument.Content.Find.Font属性,它可以设置要搜索的各种字体格式,然后就是app.ActiveDocument.Content.Find.Format = true ;
如果设置为True,才可以应用格式搜索。
但是奇怪的是,有时候我设置app.ActiveDocument.Content.Find.Format属性的时候总是设置不了,
执行了下列代码
app.ActiveDocument.Content.Find.Format = true ;
但是app.ActiveDocument.Content.Find.Format依然为False,不知道是为什么,希望有经验的高手能为我解答,谢谢!
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索app
, 搜索
, find
, doc
, content
, ref
ref()
font style、font style normal、font style italic、font style属性值、css font style,以便于您获取更多的相关知识。