VBA宏读出word文档中使用的所有字体样式

时间:2011-12-08 13:17:49

标签: vba ms-word word-vba

为了检查某些word文档的格式是否一致,我希望得到一个列表,列出所有使用/应用于某个文档的不同字体。我在GUI中找不到这个选项,但这可以通过VBA以编程方式完成吗?我没有任何VBA经验,但是这样的一些伪代码应该可以工作,对吗?

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

2 个答案:

答案 0 :(得分:3)

是的:有一个实现从文档here读取所有字体,遵循该模式。

答案 1 :(得分:0)

正在寻找类似我自己的东西:

打印字体和大小的列表

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