Back in the old days, when I was running Office 95 on top of Windows 95, I discovered that Word 95 shipped with a cool Font Sampler macro written in WordBasic. Running the macro created a document containing a sample of every font installed on the system. Unfortunately, the Font Sampler macro has been missing from every version of Word since then. Since the resulting font sample document was such a great reference tool, especially when printed, I always wondered why Microsoft had never updated the Font Sampler macro to Visual Basic for Applications (VBA) for inclusion with subsequent versions of Word.
I recently encountered a situation in Word 2002 where having such a reference tool would have been helpful. So I decided that it was time I sat down and wrote my own VBA Font Sampler macro. Before I got started, however, I decided to see if I could track down the original WordBasic version. After doing a bit of investigation, I discovered that Microsoft does indeed have a VBA version of the Font Sampler macro—it's provided as a VBA programming example in Microsoft Knowledge Base article 209205, "WD2000: Macro to Generate List of Available Fonts in Word."
But when I ran Microsoft’s new Font Sampler macro, I discovered that the resulting document left a lot to be desired. The biggest drawback is that the fonts appear in the document in a random order—they aren't sorted alphabetically. In addition, the font examples include only lowercase letters and numeric characters, which really isn’t enough to give you a good feel for how the actual font looks. As a result, I decided to delve into the code for Microsoft’s new VBA Font Sampler macro and fix these shortcomings.
A word about Word versions
The VBA Font Sampler macro works in Word 2000 and Word XP/2002. It should also work in Word 97, but I was unable to test the VBA Font Sampler macro in this version of Word. If you’re running Word 97, please post a message in the Discussion Center and let me know if the VBA Font Sampler macro works.
Studying the VBA Font Sampler macro
Let’s jump right in and take a step-by-step look at how my updated VBA Font Sampler macro works. Listing A shows my macro with each line of code numbered, making it easier to talk about.
Setting the stage
As you can see in Listing A, the macro is only 34 lines long. Line 1 uses the Sub statement to specify the beginning of the subprocedure that contains the VBA Font Sampler macro. The End Sub statement in line 34 declares the conclusion of the subprocedure.
Line 2 uses the Dim statement to declare the lone variable used by this subprocedure: TTFont. It’s declared as a Variant data type, which is basically a universal data container capable of holding all types of data. In this case, it will sequentially hold objects that represent each of the fonts installed on the system.
Before the macro actually begins processing the fonts, the display is disabled by setting the ScreenUpdating property to False in line 3. This allows the macro to perform all its work in the background without having to continuously update the screen. This essentially improves the performance of the macro. The ScreenUpdating property is set back to True in line 34, at which point the macro has completed its job and the sorted list of fonts is displayed in a new document.
The Documents.Add Template:="normal" command in line 4 uses the Add method of the Documents object to create a new document based on the Normal.dot template. This is the document that will ultimately contain the sorted list of fonts.
Extracting the fonts
Line 5 begins a For…Next looping structure that runs through line 29 and allows the macro to sequentially process each font. To do so, the first thing that the For…Next looping structure does is access the FontNames object, which basically contains a list of the names of all available fonts, and assign the name of a font to the TTFont variable. Then, the With Selection statement, which is designed to perform a series of statements on a selected object without having to requalify the name of the object, takes the font name and applies it to each of the statements in between lines 6 and 28. As you can see, this is where the bulk of the macro’s job is performed.
Creating the examples
Lines 7 thru 11 set up the header for each font example in the document. This header will consist of the name of the font formatted using 18-point Arial font boldfaced and underlined. Lines 7 thru 10 use the Font property to specify the formatting, and line 11 uses the TypeText method to type the name of the font.
In line 12, I use the TypeText method to issue an odd character called a vertical tab. The use of the vertical tab character is the key to being able to sort the list of fonts in the document in alphabetical order. I’ll explain in more detail how this works in a moment.
Lines 13 thru 15 use the Font property to reconfigure the text formatting from the header format to the actual example format, which is basically 12-point regular. Line 16 activates the selected font.
Then, in lines 17 thru 25, I use the TypeText method to create each of the example lines, which consist of uppercase and lowercase alphabetical characters, numeric characters, and the classic font example pangram. You’ll see that each of these example lines is separated by vertical tab characters.
What’s a pangram?
A pangram is a grammatically complete sentence that uses every letter of the alphabet only once. The classic pangram used by typographers is, “The quick brown fox jumps over a lazy dog.” But even though this pangram uses all 26 letters of the alphabet in a grammatically complete sentence, it misses the only once specification by nine letters, as it is actually a 35-letter sentence. The letters o, u, e, r, t, and h are used more than once.
In line 26, the InsertParagraphAfter method inserts a paragraph mark in the document at the end of the example. Then, in line 27, the MoveDown method, moves the insertion pointer down to the next line, where the next example will be placed when the loop continues. Lines 28 and 29 then mark the end of the loop as I explained earlier.
The significance of the vertical tab character
Before we move on, let’s take a moment to investigate the significance of the vertical tab character. As I mentioned, this odd character is the key to being able to sort the list of fonts in the document in alphabetical order.
The vertical tab character is unique in that it moves the insertion pointer down to the next physical line in a document without actually issuing a carriage return or a line feed. As you’ve seen, I used vertical tab characters in between each part of the example text. This allows me to format each font example so that it appears on a separate line, when in reality it’s all on a single line. I then separate each example with a paragraph marker via the commands in lines 26 and 27.
This extravagant formatting technique tricks Word’s Sort feature, which, by default, sorts the contents of a document based on the first letter of the first word that appears on each line. Since the entire example is actually on one line, Word’s Sort feature will pay attention only to the first letter in the name of the font when performing the sort operation. The rest of the example text simply follows along.
Presenting the document
The presentation of the resulting document is performed in lines 30 thru 33. In line 30, the WholeStory method selects the document. Then, the Sort method in line 31 sorts the document in ascending alphabetical order. The HomeKey method in line 32 moves the insertion point to the top of the document. Finally, the ScreenUpdating property is set to True in line 33, and the document becomes visible.
Adding the VBA Font Sampler macro to Word
Now that you have a good understanding of how the VBA Font Sampler macro works, let’s take a look at how you go about adding it to Word. To begin with, since you’ll use this macro only once in a while, I recommend that you save it in its own template. That way, it is isolated and can’t interfere with any other document or macro you create.
Start by launching Word, pulling down the File menu, and selecting the Save command. When you see the Save As dialog box, type FontSampler in the File Name text box, select Document Template from the Save As Type drop-down list, and click the Save button.
To add the VBA Font Sampler macro to the FontSampler.dot template, pull down the Tools menu, open the Macro submenu, and select the Macros command. When you see the Macros dialog box, type FontSampler in the Macro Name text box and select the FontSampler.dot template from the Macros In drop-down list, as shown in Figure A.
|You’ll use the Macros dialog box to save the VBA Font Sampler macro in the FontSampler.dot template.|
To continue with the procedure, click Create to open the Visual Basic editor. Select and delete the text that appears in the main window. Then, open the sidebar in Listing B, which contains the macro (without line numbers). Select all the code in the listing (avoid selecting the title), and press[Ctrl]C to copy it to the clipboard. Switch back to the Visual Basic editor, select the main window, and press [Ctrl]V to paste the text of VBA Font Sampler macro into the window, as shown in Figure B.
|You can paste the VBA Font Sampler macro into the Visual Basic editor’s main window.|
Now, pull down the File menu and select the Close And Return To Microsoft Word command. When you do so, the Visual Basic editor will close and you’ll again see your blank FontSampler.dot template. To complete the operation, pull down the File menu, and select the Save command. Then, close the FontSampler.dot template.
Running the VBA Font Sampler macro
Running the VBA Font Sampler macro is easy once you’ve added it to the FontSampler.dot template. First, pull down the File menu, choose the New command, and select the FontSampler.dot template. Next, pull down the Tools menu, open the Macro submenu, and select the Macros command. When you see the Macros dialog box, the FontSampler macro will be selected by default and all you need to do is click the Run button.
In a moment, you’ll see a new document containing examples of all the fonts you have installed on your system, as shown in Figure C. You can then print the document so you have a hard copy reference of all your fonts. You can also save the document on your hard disk for future reference.
|Running the VBA Font Sampler macro will produce a document containing examples of all the fonts you have installed on your system.|
Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.