Microsoft Word Conditional Formatting VBA code example

Microsoft Word Conditional Formatting VBA code example

Condition

A table object can be decorated using conditional formatting. Condition method exposed by Style object, provides various formatting properties and methods like shedding, borders, font, paragraph formatting etc. Each property driven by own set of constants.

Source table

Code example

Public Sub ConditionalStyleExample()
    'Declare object to hold a document
    Dim oDoc As Document
    
    'Reference active document
    Set oDoc = ActiveDocument
    
    'Declare table object
    Dim oTable As Table
    
    'Bind first table reference
    Set oTable = oDoc.Tables(1)
    
    'Select table
    oTable.Select
    
    'Perform conditinoal formatting
    oTable.Style.Table.Condition(wdFirstRow).Shading.BackgroundPatternColor = wdColorDarkBlue
    oTable.Style.Table.Condition(wdOddRowBanding).Shading.BackgroundPatternColor = wdColorGray10oDoc
    oTable.Style.Table.Condition(wdLastRow).Borders(wdBorderTop).LineStyle = wdLineStyleDouble
    oTable.Style.Table.Condition(wdLastRow).Borders(wdBorderBottom).LineStyle = wdLineStyleDouble
    
    'Memeory Cleanup
    Set oTable = Nothing
    Set oDoc = Nothing
    
End Sub

Output

Please leave your comments or queries under comment section also please do subscribe to our blogs to keep your self upto date.

Leave a Reply

Your email address will not be published. Required fields are marked *