MS Word

Apply Proper Capitalization

The Title Case option in the Change Case dialog box (Format » Change Case) makes the first letter of each word uppercase and the remaining letters lowercase. But conjunctions and short prepositions (five letters or fewer, according to the magazine I work for) should be all lowercase.

A macro can fix most of these errors, although you will need to check prepositions that need initial capitals because they are part of a verb. Example 8-8 shows the macro.

If you're familiar with VBA, you'll probably find the extended Or condition cumbersomenot to mention a lot of typing. You can make the code more compact by using a Select Case statement instead of the if statement, using Case "a", "above", "after", "an", and so on.

The code in the macro is fairly straightforward:

  • In the first group of statements, the macro declares two variables: a String variable named strL and an Integer variable named i.

  • In the second group of statements, the macro selects the appropriate paragraph. The first statement compares the selection type to wdInsertionPoint the "selection" being a point in the text rather than an actual selection of characters or other objects. If the selection is not an insertion point, the macro collapses the selection to an insertion point. The second statement selects the first paragraph of the (collapsed) selectionin other words, the paragraph in which the insertion point is located. Selecting the paragraph also selects the paragraph mark at its end, and this paragraph mark counts as a "word." So, to work with only the real words, the third statement shortens the selection by one character to the left, deselecting the paragraph mark.

  • The third group of statements applies title case (first letter capitalized) to the first and last words in the selection, because the first and last words in a heading must always be capitalized even if they're short prepositions or conjunctions.

  • The fourth group of statements uses a For... Next loop to check each of the remaining words in the selection against a list of short prepositions and conjunctions. (Adjust this list to meet your company's style demands.) The loop runs from 2 (the second word) to Selection.Words.Count - 1, or the number of words in the paragraph minus 1 (so as not to affect the last word, to which title case has already been applied). The second statement assigns to strL the lowercase (LCase) version of the currently selected word, with any leading or trailing spaces trimmed off it (when you double-click to select a word, VBA includes any spaces after the word). Using the lowercase version of the word is necessary because the comparison is case-sensitive. The If statement then compares strL to the list of prepositions and conjunctions. If there's a match, Word applies lowercase to the word; if not, the Else statement applies title case to the word.

  • Finally, the Selection.Collapse statement collapses the selection to its end, which is equivalent to pressing » to deselect a selection.

Macro to apply proper capitalization to a title

 Sub Apply_Proper_Capitalization()
   'applies proper capitalization to the first paragraph in the selection
    Dim strL As String
    Dim i As Integer
    If Selection.Type <> wdSelectionIP Then Selection.Collapse
    Selection.Paragraphs(1).Range.Select
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Words(1).Case = wdTitleWord
    Selection.Words(Selection.Words.Count).Case = wdTitleWord
    For i = 2 To Selection.Words.Count - 1
      strL = LCase(Trim(Selection.Words(i)))
      If strL = "a" Or strL = "above" Or strL = "after" Or strL = "an" Or _
      strL = "and" Or strL = "as" Or strL = "at" Or strL = "below" Or _
      strL = "but" Or strL = "by" Or strL = "down" Or strL = "for" Or _
      strL = "from" Or strL = "in" Or strL = "into" Or strL = "of" Or _
      strL = "off" Or strL = "on" Or strL = "onto" Or strL = "or" Or _
      strL = "out" Or strL = "over" Or strL = "the" Or strL = "to" Or _
      strL = "under" Or strL = "up" Or strL = "with" Then
        Selection.Words(i).Case = wdLowerCase
      Else
        Selection.Words(i).Case = wdTitleWord
     End If
  Next i
  Selection.Collapse Direction:=wdEnd
End Sub

This macro isn't perfect, as it ignores the possibility of periods or other punctuation requiring prepositions or conjunctions to be title case in the middle of a paragraph. You might adapt the macro to take care of this need. You might also add a list of uppercase terms (for example, XP) or intercapitalized terms (for example, iPod) that require special treatment.