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

I am using something like this:

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

but when I go through the code, the Name property does not change. Thank you


I work with VS2010 and MS Word 2007, and I want to find and replace all Times New Roman "Arial" fonts.

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
source share
2 answers

Thank you for your answer, but no, you do not get a new Find object every time you use dot notation. The problem is that you should not use Doc.Content.Find in a similar situation. Instead, you need to create a new Range object and use its Find. Something like that:

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

I believe that you need to get the FIND object, and then use it when you reference the object through dotted notation, as with you, you always get a completely new FIND object, so you lose your settings every time.

Something like that

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

Source: https://habr.com/ru/post/1388860/


All Articles