Search code examples
vb.netvisual-studio-2010ms-wordoffice-interop

Checking Font Styles In Word Document Using VB.NET


i want to check a word file using vb.net and check that the styles in the document are proper or not.

I have to check for these expressions in word document

a.Verdana + 16 pt + Bold + Red

b.Verdana + 12 pt + Bold + Italic + Blue

c.Verdana + 11 pt + Bold + Italic + Brown

d.Arial + 10 pt + Black

I have tried this,

If objDoc.Range.Font.Name = "Arial" And objDoc.Range.Font.Size = 10 Then
   If objDoc.Range.Font.Color = WdColor.wdColorBlack Then
   End If
        MsgBox("ok")
    Else
        MsgBox("not ok")
End If

But with this code it shows msgbox "OK" only when the whole word document consist of Arial,10,Black and shows msgbox "Not Ok" when it consist the above expressions except for the Arial,10,Black

So basically i need help to find out all the expressions in the same word document which consist of all the above expressions/Styles.

Any Help will be really really appreciable.. Plz help me with this still not able to find a solution..


Solution

  • With the foolowing code you can find sentences where font style is different.

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim oDoc As New Word.Document()
        Dim wapp As New Word.Application()
    
        Try
            oDoc = wapp.Documents.Open(TextBox1.Text & "\" & "TEST.doc")
    
            For Each Senetence As Word.Range In oDoc.Sentences
                For Each Character As Word.Range In Senetence.Characters
                    If Character.Font.Name <> "Verdana" AndAlso Character.Font.Name <> "Arial" Then
                        MsgBox(" Font Name not matching Error Line number " & Senetence.Text)
                        Exit For
                    End If
                Next
            Next
            oDoc.Close()
        Catch ex As Exception
            oDoc.Close()
        End Try
    
    End Sub