如何使用iterop在Word文档中搜索特定的字体 [英] How to search for a specific font in a Word document with iterop
问题描述
我使用类似这样的内容:
doc.Content.Find.Font.Name =Times New Roman;
但是当我遍历代码时,Name属性不会改变。
谢谢。
我正在使用VS2010和MS Word 2007,我想查找并替换所有Times New Roman字体与Arial。
以下是发生的情况:
Word。应用程序wordApp = new Word.Application();
Word.Documents docs = wordApp.Documents;
doc = docs.Open(fileName,Visible:false);
doc.Content.Find.ClearFormatting();
doc.Content.Find.Replacement.ClearFormatting();
//这里Find.Font.Name和Replacement.Font.Name的值是
doc.Content.Find.Font.Name =Times New Roman ;
doc.Content.Find.Replacement.Font.Name =Arial;
// Find.Font.Name和Replacement.Font.Name的值仍然是!!!
doc.Content.Find.Execute(Format:true,Replace:Word.WdReplace.wdReplaceAll);
感谢您的回复,但是不每次使用点符号时都会得到一个新的Find对象。问题是你不应该在这种情况下使用Doc.Content.Find。相反,您必须创建一个新的Range对象并使用它的Find。像这样:
Word.Range range = doc.Range(0,doc.Content.End);
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);
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);
这篇关于如何使用iterop在Word文档中搜索特定的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!