Jaa


Solution - Novice Challenge 4: I Need a New Style

Our initial idea for this challenge was to have you set the font in the entire document to the first font on the local computer (alphabetically speaking) that begins with the letter B. However, we changed our minds about the alphabetically part; we’ll explain why when we take you through the solution we came up with.

Which, by the way, is this:

Sub ChangeStyleToB()

 

Dim str As Variant

 

'Cycle through all the font names on the computer.

For Each str In FontNames

 

    'If the font start with a B, select the entire doc and

    ' change the font name to the current font.

    If InStr(1, UCase(str), "B") = 1 Then

        ActiveDocument.Select

        Selection.Font.Name = str

        Exit Sub

    End If

 

Next

 

End Sub

 

After declaring one variable (str), we launch right in and start up a For loop:

For Each str In FontNames

 

In this For loop we’re looping through all the items in the FontNames collection. The FontNames collection contains a list of all the fonts installed on the computer on which the macro is being run. Now here’s the interesting part: the fonts are not returned in alphabetical order. We’re not exactly sure what order they are returned in, but it’s definitely not alphabetical. So rather than complicate this challenge by making you sort the results, we decided to skip the alphabetical thing altogether. Instead we just want to find the first font name beginning with a B that FontNames decides to return. And we do that like this:

If InStr(1, UCase(str), "B") = 1 Then

To determine whether a font name begins with the letter B, we call the InStr function. The InStr function determines whether a substring exists within a given string. InStr takes three parameters:

· Start – The position within the string at which you want to start searching. We’re going to start at the beginning of the string, so we use a value of 1.

· String1 – The string we’re going to search. The string we want to search is the font name, contained in the variable str. Font names won’t necessarily start with an uppercase letter (or a lowercase letter, for that matter), so we use the UCase function to turn the entire string into all uppercase.

· String2 – The substring we’re looking for. We want to find a font name beginning with a B, so we check to see whether there is a B in the name.

It’s nice that InStr will look for the letter B anywhere in the string, but we only care if B is the first character in the string. Well, the cool thing about InStr is this: if it finds the substring within the string, it tells you the position within the string where it was found. That’s why we check to see if the return value of InStr is a 1: if it is, the letter B was found in position 1 of the string. In other words, if the return value is 1 then the font name in the string begins with a B.

Once we’ve found a font name beginning with B we need to change the font of all text within the document to that font. To do that we first use the Select method of the current document to select all the text:

ActiveDocument.Select

After we’ve selected the text, it’s a simple matter of setting the Name property on the Font object for that selection to the font name in stored in the variable str:

Selection.Font.Name = str

At this point all the text in our document has the new font. That means we can stop looking for fonts beginning with the letter B and call Exit Sub to end our subroutine.

And we’re done. On our test machine the first B font turned out to be Batang:

Renamed Font