如何使用iterop在Word文档中搜索特定的字体 [英] How to search for a specific font in a Word document with iterop

查看:190
本文介绍了如何使用iterop在Word文档中搜索特定的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用类似这样的内容:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆