如何使用 iterop 在 Word 文档中搜索特定字体

发布于 2024-10-06 23:24:03 字数 789 浏览 0 评论 0原文

我使用这样的东西:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆橘 2024-10-13 23:24:03

感谢您的回复,但是每次使用点表示法时,您不会得到一个新的 Find 对象。问题是在这种情况下你不应该使用 Doc.Content.Find 。相反,您必须创建一个新的 Range 对象并使用它的 Find。像这样的东西:

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

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);
花伊自在美 2024-10-13 23:24:03

我相信你需要获取一个 FIND 对象然后使用它,当你像你一样通过点符号引用该对象时,你总是会得到一个全新的 FIND 对象,所以你每次都会丢失你的设置。

像这样的东西

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

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
忆梦 2024-10-13 23:24:03

我用过这个:

Microsoft.Office.Interop.Word._Application word;
Microsoft.Office.Interop.Word._Document doc;

bool found_next = false;
private void search_Replace1()
{
    word = Globals.ThisAddIn.Application;
    doc = word.ActiveDocument;
    word.Selection.Find.Font.Name = "My Font";
    found_next= word.Selection.Find.Execute(Format: true);
    if (found_next)
    {
        word.Selection.Font.Name = "Arial"; 
        //word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdRed;  //change color to red
    }
}

I used this:

Microsoft.Office.Interop.Word._Application word;
Microsoft.Office.Interop.Word._Document doc;

bool found_next = false;
private void search_Replace1()
{
    word = Globals.ThisAddIn.Application;
    doc = word.ActiveDocument;
    word.Selection.Find.Font.Name = "My Font";
    found_next= word.Selection.Find.Execute(Format: true);
    if (found_next)
    {
        word.Selection.Font.Name = "Arial"; 
        //word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdRed;  //change color to red
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文