How to search for a specific font in a Word document with iterop

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I use something like this:

doc.Content.Find.Font.Name = "Times New Roman"; 

but when I step through the code the Name property doesn't change. thanks.


I'm working with VS2010 and MS Word 2007 and I want to find and replace all "Times New Roman" fonts with "Arial".

Here's what happens:

Word.Application wordApp = new Word.Application(); Word.Documents docs = wordApp.Documents; doc = docs.Open(fileName, Visible: false); doc.Content.Find.ClearFormatting(); doc.Content.Find.Replacement.ClearFormatting();  // Here the value of Find.Font.Name and Replacement.Font.Name is ""  doc.Content.Find.Font.Name = "Times New Roman"; doc.Content.Find.Replacement.Font.Name = "Arial";  // The value of Find.Font.Name and Replacement.Font.Name still "" !!!  doc.Content.Find.Execute(Format: true, Replace: Word.WdReplace.wdReplaceAll); 

回答1:

Thanks for your reply, but no you don't get a new Find object each time you use dot notation. The problem is you shouldn't use Doc.Content.Find in this kind of situation. Instead you have to create a new Range object and use its Find. Something like this:

Word.Range range = doc.Range(0, doc.Content.End); 


回答2:

I believe you need to obtain a FIND object and then use it, when you refer to the object via dot notation like you have, you're always getting a brand new FIND object, so you'll loose your settings each time.

Something like this

With Doc.content.Find     .clearFormatting     .Font.name = "blah"     .Execute ..... End With 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!