views:

285

answers:

4

For some automated tests I need to create a Word Doc that contains all the characters of a font (for several different fonts). Is there an easy way to create a macro that loops through all the available characters in a font (and inserts them into a doc)?

+1  A: 

Geez... I've made something like that a long time ago... Yes, it's possible to do it.

A good start is the MSDN


Edited to add:

I knew I had done something like this before. Going through some of my old emails I found a macro I've sent to a friend of mine containing exactly this. Here it is:

Sub GenerateFontCatalog()
'
' Macro created in 05/14/2008 by Paulo Santos
'
Dim i As Long
Dim j As Long
Dim fnt As String
Dim doc As Document
Dim fnts() As String

'*
'* Get all font names
'*
Word.StatusBar = "Reading Font Names..."
ReDim fnts(Word.FontNames.Count)
For i = 1 To Word.FontNames.Count
    fnts(i) = Word.FontNames.Item(i)
    DoEvents
Next

'*
'* Sort alphabetically
'*
Word.StatusBar = "Sorting Font Names..."
For i = 1 To UBound(fnts)
    For j = i + 1 To UBound(fnts)
        If (fnts(i) > fnts(j)) Then
            fnt = fnts(i)
            fnts(i) = fnts(j)
            fnts(j) = fnt
        End If
    Next
    DoEvents
Next

Word.StatusBar = "Generating Font Catalog..."

Set doc = Application.Documents.Add()
doc.Activate

'*
'* Page configuration
'*
With ActiveDocument.PageSetup
    .Orientation = wdOrientPortrait
    .TopMargin = CentimetersToPoints(2)
    .BottomMargin = CentimetersToPoints(2)
    .LeftMargin = CentimetersToPoints(2)
    .RightMargin = CentimetersToPoints(2)
End With

For i = 1 To UBound(fnts)
    '*
    '* Write font name
    '*
    Selection.Font.Name = "Arial"
    Selection.Font.Size = 10
    If (i > 1) Then
        Selection.TypeParagraph
        Selection.ParagraphFormat.KeepTogether = False
        Selection.ParagraphFormat.KeepWithNext = False
        Selection.TypeParagraph
    End If
    Selection.TypeText fnts(i)
    Selection.ParagraphFormat.KeepWithNext = True
    Selection.TypeParagraph

    '*
    '* Write font sample
    '*
    Selection.Font.Name = fnts(i)
    Selection.Font.Size = 16
    Selection.TypeText "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & Chr(11)
    Selection.TypeText "abcdefghijklmnopqrstuvwxyz" & Chr(11)
    Selection.TypeText "0123456789"
    Selection.ParagraphFormat.KeepTogether = True

    DoEvents
Next

'*
'* Adjust cursor position
'*
Selection.HomeKey Unit:=wdStory
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1

Word.StatusBar = "Generating Font Index..."
For i = 1 To UBound(fnts)
    Selection.Font.Name = "Arial"
    Selection.Font.Size = 10
    Selection.TypeText fnts(i) & vbTab
    Selection.Font.Name = fnts(i)
    Selection.TypeText "ABC abc 123"
    Selection.TypeParagraph
Next

'*
'* Split the document in two columns
'*
With Selection.Sections(1).PageSetup.TextColumns
    .SetCount NumColumns:=2
    .EvenlySpaced = True
    .LineBetween = False
End With
Selection.HomeKey Unit:=wdStory, Extend:=True
Selection.ParagraphFormat.TabStops.Add Position:=Selection.Sections(1).PageSetup.TextColumns(1).Width, Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces

Selection.HomeKey Unit:=wdStory
Word.StatusBar = ""

End Sub
Paulo Santos
I don't really need a list of fonts but a list of all characters in a font.
leonm
Nice macro (even though it doesn't exactly what the requestor wanted in this particular case) - thanks for sharing!
monojohnny
A: 

Is there a particular reason why it needs to be in Word? Is this simply to visualize all the characters of a font? If so, you can use Font Book in OS X and go to Print, select Report Type: Repetoire, and save out to PDF.

Edit: Seems I missed "automated tests". Disregard.

Tegeril
A: 

Seems like a pretty good compromise is to create an html file with &#XXX; entries for each character and then open that with MS Word.

leonm
Why the downvote? It sounds like perfectly acceptable solution, especially because you can't get to the list of characters in Word.
leonm
+1  A: 

It will be nice idea to insert

The quick brown fox jumps over the lazy dog

and loop through the fonts that need to be tested using a macro.

Thunder
http://en.wikipedia.org/wiki/The_quick_brown_fox_jumps_over_the_lazy_dog can viewed for more details
Thunder