Word允许您使用正在使用的系统上安装的字体。字体安装在Windows中,因此它们不仅可用于Word,而且可用于系统上安装的所有程序。

在系统上创建文档时,很容易知道正在使用什么字体-字体列表仅限于系统上可用的字体。但是,如果您从另一个人那里收到文档,则另一个人的系统可能安装了与您不同的字体。这意味着他们的Word文档可以使用您系统上没有的字体进行格式化。

如果要生成文档中使用的字体列表(而不是系统上可用的字体列表),则有两种选择。首先,您可以在文本编辑器中打开Word文档,并浏览在Word中通常看不到的文档部分。在文件末尾附近,您应该看到文档中使用的字体列表。但是,如果这样做,则应非常小心,不要在文本编辑器中打开Word文档时对其进行任何更改。这样做很容易使文档不再在Word中可用。

基于Word的解决方案是简单地浏览文档中的每个字符,然后检查用于格式化字符的字体。逐个字符的方法是必需的,因为每个字符可以使用不同的字体设置格式,并且VBA不允许您访问与文档本身有关的字体集合-似乎没有这样的集合。因此,最安全(也是最慢)

方法是简单地浏览每个字符并创建自己的列表。下面的VBA宏可以完成任务:

Public Sub ListFontsInDoc1()

Dim FontList(199) As String   Dim FontCount As Integer   Dim FontName As String   Dim J As Integer, K As Integer, L As Integer   Dim X As Long, Y As Long   Dim FoundFont As Boolean   Dim rngChar As Range   Dim strFontList As String

FontCount = 0   X = ActiveDocument.Characters.Count   Y = 0   ' For-Next loop through every character   For Each rngChar In ActiveDocument.Characters     Y = Y + 1     FontName = rngChar.Font.Name     StatusBar = Y & ":" & X     ' check if font used for this char already in list     FoundFont = False     For J = 1 To FontCount       If FontList(J) = FontName Then FoundFont = True     Next J     If Not FoundFont Then       FontCount = FontCount + 1       FontList(FontCount) = FontName     End If   Next rngChar

' sort the list   StatusBar = "Sorting Font List"

For J = 1 To FontCount - 1     L = J     For K = J + 1 To FontCount       If FontList(L) > FontList(K) Then L = K     Next K     If J <> L Then       FontName = FontList(J)

FontList(J) = FontList(L)

FontList(L) = FontName     End If   Next J

StatusBar = ""

' put in new document   Documents.Add   Selection.TypeText Text:="There are " & _    FontCount & " fonts used in the document, as follows:"

Selection.TypeParagraph   Selection.TypeParagraph   For J = 1 To FontCount     Selection.TypeText Text:=FontList(J)

Selection.TypeParagraph   Next J End Sub

显然,文档越长,完成宏所需的时间越长。 (我在1100页的文档上运行了宏,大约花费了46分钟。在五页的文档上,花费了不到一分钟。)完成后,宏创建了一个新文档,其中包含所用字体的排序列表。

上面的宏仅遍历主文档。文档的其他元素中可能还会使用其他不同的字体。如果希望将这些元素包括在列表中,则需要在宏上使用一个变体,将这些其他元素考虑在内。下面的宏(ListFontsInDoc2)更长,该清单还包括从主宏中调用的其他三个宏。

Public Sub ListFontsInDoc2()

Dim rngStory As Word.Range   Dim rngChar As Range   Dim oShp As Word.Shape   Dim FontName As String   Dim lngIndex As Long   Dim lngChar As Long   Dim lngCharCount As Long   Dim colFontsUsed As New Collection   Dim oDocList As Word.Document

For Each rngStory In ActiveDocument.StoryRanges     lngChar = 0     lngCharCount = rngStory.Characters.Count     Do       'Evaluate each character       Set rngChar = rngStory.Characters(1)

If rngStory.End > 1 Then         Do           lngChar = lngChar + 1           FontName = rngChar.Font.Name           StatusBar = "Evaluauting character " & lngChar & _            " of " & lngCharCount & " characters in the story range"

'Check if font used for this character already in list           On Error Resume Next           'Collection key prevents adding fonts already           'in the collection           colFontsUsed.Add rngChar.Font.Name, rngChar.Font.Name           On Error GoTo 0           rngChar.MoveStart wdCharacter, 1           rngChar.MoveEnd wdCharacter, 1           'Set rngChar = rngChar.Next '         Loop Until rngChar.End = rngStory.End       End If

'Evaluate shapes in headers and footers       Select Case rngStory.StoryType         Case 6, 7, 8, 9, 10, 11           'No shape will throw an error that we handle and skip           On Error GoTo Err_Handler           If rngStory.ShapeRange.Count > 0 Then             For Each oShp In rngStory.ShapeRange               If oShp.TextFrame.HasText Then                 lngChar = 0                 lngCharCount = oShp.TextFrame.TextRange.Characters.Count                 For Each rngChar In oShp.TextFrame.TextRange.Characters                   lngChar = lngChar + 1                   FontName = rngChar.Font.Name                   StatusBar = "Evaluauting character " & _                    lngChar & " of " & lngCharCount & _                    " characters in the story range"

On Error Resume Next                   colFontsUsed.Add rngChar.Font.Name, rngChar.Font.Name                   On Error GoTo 0                 Next rngChar               End If             Next oShp           End If         Case Else           'Do Nothing       End Select

SkipRange:

On Error GoTo 0       'Get next linked story (if any)

Set rngStory = rngStory.NextStoryRange     Loop Until rngStory Is Nothing   Next rngStory   'Sort the collection.

StatusBar = "Sorting Font List"

Set colFontsUsed = SortCollection(colFontsUsed)

StatusBar = ""

'Create font list document.

Set oDocList = Documents.Add   With oDocList.Range     .Text = "There are " & colFontsUsed.Count & _      " fonts used in the document, as follows:" & vbCr & vbCr     For lngIndex = 1 To colFontsUsed.Count       .InsertAfter colFontsUsed(lngIndex) & vbCr     Next lngIndex   End With   Set oDocList = Nothing   Exit Sub

Err_Handler:

Resume SkipRange End Sub
Public Function SortCollection(ByVal oCol As Collection) As Collection   Dim arrIndex() As Long   Dim lngCount As Long   Dim i As Long   Dim m As Long   Dim oColSorted As New Collection

lngCount = oCol.Count   If lngCount = 0 Then     Set SortCollection = New Collection     Exit Function   End If

'Allocate an index array.

ReDim arrIndex(0 To lngCount - 1) As Long   'Fill the index array.

For i = 0 To lngCount - 1     arrIndex(i) = i + 1   Next i

'Generate an ordered heap   For i = lngCount/2 - 1 To 0 Step -1     Heapify oCol, arrIndex, i, lngCount   Next i

'Sort the index array   For m = lngCount To 2 Step -1     Exchange arrIndex, 0, m - 1     Heapify oCol, arrIndex, 0, m - 1   Next   For i = 0 To lngCount - 1     oColSorted.Add oCol.Item(arrIndex(i))

Next ' fill output collection   Set SortCollection = oColSorted End Function
Private Sub Heapify(oCol As Collection, arrIndexPasssed() As Long, _  lngIndex As Long, lngCount As Long)

Dim lngMidCount As Long   Dim i As Long   lngMidCount = lngCount/2

Do While lngIndex < lngMidCount     i = 2 * lngIndex + 1     If i + 1 < lngCount Then       If oCol.Item(arrIndexPasssed(i)) < oCol.Item(arrIndexPasssed(i + 1)) _        Then i = i + 1     End If     If oCol.Item(arrIndexPasssed(lngIndex)) >= oCol.Item(arrIndexPasssed(i)) _      Then Exit Do     Exchange arrIndexPasssed, lngIndex, i     lngIndex = i   Loop End Sub
Private Sub Exchange(Index() As Long, i As Long, j As Long)

Dim Temp As Long   Temp = Index(i)

Index(i) = Index(j)

Index(j) = Temp End Sub

注意:

如果您想知道如何使用此页面(或_WordTips_网站上的任何其他页面)上描述的宏,我准备了一个包含有用信息的特殊页面。

_WordTips_是您进行经济有效的Microsoft Word培训的来源。

(Microsoft Word是世界上最流行的文字处理软件。)本技巧(1522)适用于Microsoft Word 97、2000、2002和2003。您可以在Word(Word 2007)的功能区界面中找到此技巧的版本。和更高版本)在这里: