memyself
memyself

Reputation: 12618

VBA macro to print all font styles used in a word document

in order to check some word documents for consistent formatting, I would love to get a list of all the different fonts that are used / applied to a certain document. I couldn't find an option for this in the GUI, but could this be done programmatically with VBA? I don't have any VBA experience, but some pseudo code like this should work, right?

all_fonts = []
for paragraph in all_paragraphs
    for each word in paragraph
       if word.style not in all_fonts
           all_fonts.append(word.style)


print all_fonts

Upvotes: 1

Views: 4297

Answers (2)

Mikku
Mikku

Reputation: 6654

Was looking for something like this Myself:

Prints a list of Fonts and the Sizes

Sub Font_Size()

Dim lw() As Variant
Dim exists As Boolean
Dim stry As Object
Dim sen As Object
Dim wd As Object

ReDim Preserve lw(1 To 2, 1 To 1): lw(1, 1) = "Size": lw(2, 1) = "Font Name"

For Each stry In ActiveDocument.StoryRanges

    For Each sen In stry.Sentences

        For Each wd In sen.Words

                exists = False

                For i = LBound(lw, 2) To UBound(lw, 2)

                    If wd.Font.Size = lw(1, i) And wd.Font.Name = lw(2, i) Then
                        exists = True
                        Exit For
                    End If

                Next

                If Not exists Then

                        ReDim Preserve lw(1 To 2, 1 To UBound(lw, 2) + 1)
                        lw(1, UBound(lw, 2)) = wd.Font.Size
                        lw(2, UBound(lw, 2)) = wd.Font.Name
                        'Debug.Print lw(1, UBound(lw, 2)), lw(2, UBound(lw, 2))

                End If

        Next

    Next

Next


For i = LBound(lw, 2) To UBound(lw, 2)
    Debug.Print lw(1, i), lw(2, i)
Next

End Sub

Upvotes: 0

stuartd
stuartd

Reputation: 73243

Yes: there's an implementation of reading all fonts from a document here which follows that pattern.

Upvotes: 3

Related Questions