如何使用 iterop 在 Word 文档中搜索特定字体
我使用这样的东西:
doc.Content.Find.Font.Name = "Times New Roman";
但是当我单步执行代码时,Name 属性不会改变。 谢谢。
我正在使用 VS2010 和 MS Word 2007,我想查找所有“Times New Roman”字体并将其替换为“Arial”。
发生的情况如下:
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您的回复,但是每次使用点表示法时,您不会得到一个新的 Find 对象。问题是在这种情况下你不应该使用 Doc.Content.Find 。相反,您必须创建一个新的 Range 对象并使用它的 Find。像这样的东西:
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:
我相信你需要获取一个 FIND 对象然后使用它,当你像你一样通过点符号引用该对象时,你总是会得到一个全新的 FIND 对象,所以你每次都会丢失你的设置。
像这样的东西
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
我用过这个:
I used this: