Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting text by font in Word

Tags:

c#

ms-word

Is there a way of extracting all lines that are using a particular font (size, is it bolded, font name, etc) in word via C#?

In addition, is there a way to find out what is the font for some text that is in the document?

My hunch is that there are functions in the Microsoft.Office.Interop.Word namespace that can do this, but I cannot seem to find them.

Edit: I am using word 2010.

like image 933
soandos Avatar asked Nov 05 '22 08:11

soandos


1 Answers

You can loop through the document using the Find object from Word Interop. You can set the Find.Font.Name property for a Selection or Range from your document. Note that the Font interface has several Name* properties for various encodings.

EDIT

Here's the equivalent VBA code:

Dim selectionRange As Range
Set selectionRange = Application.ActiveDocument.Range

With selectionRange.Find
    .ClearFormatting
    .Format = True
    .Font.NameBi = "Narkisim" //for doc without bidirectional script, use Name
    Do While .Execute
        MsgBox selectionRange.Text
    Loop
End With

The object model from Word Interop is the same, see the link above.

Don't go asking me for C# code now... this is SO, we don't do silver platters. And if you're ever going to do serious work with the Office Interop API, you will need to be able to read VBA code.

like image 170
sq33G Avatar answered Nov 28 '22 22:11

sq33G