List Font Name Word Add-in VBA (Visual Basic for Applications) example


Object contains list of available font name in current Word Document. Following example will create a table at run time and populate each font name dynamically.

Code example

Public Sub ListFontNames()
    Dim oTable As Table
    Dim oRow As Long
    Dim oCol As Long
    'Range object
    Dim oRange As Range
    'Grab selection
    Set oRange = Selection.Range
    'Insert table and populate number of rows x 5 columns each
    Set oTable = Selection.Tables.Add(oRange, (FontNames.Count \ 5) + 1, 5)
    'Put border
    oTable.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
    oTable.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
    oTable.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
    oTable.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
    oTable.Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
    oTable.Borders(wdBorderVertical).LineStyle = wdLineStyleSingle
    'Iterate font name and write into table
    Dim oFontName
    oCol = 1
    oRow = 1
    For Each oFontName In FontNames
        oTable.Cell(oRow, oCol).Range.Text = oFontName
        If oCol > 4 Then
            oCol = 1
            oRow = oRow + 1
            oCol = oCol + 1
        End If
    Next oFontName
    If Not oRange Is Nothing Then
        Set oRange = Nothing
    End If
    If Not oTable Is Nothing Then
        Set oTable = Nothing
    End If
End Sub


Count property

Public Sub FontNameCount()
    MsgBox "Total available font count in active document is : " & FontNames.Count
End Sub


Item method

Public Sub FontNameUsingItemMethod()
    MsgBox "Selected font name is : " & FontNames.Item(5)
End Sub


Next >> Conditional Formatting Microsoft Word Add-in with example

Leave a Reply

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