简体   繁体   中英

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

是的:有从文件读取的所有字体的实现在这里它遵循的模式。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM