h1

Word: Macro to change first letter after a colon to upper case

July 18, 2011

Scenario

You have a Word document where one or more authors have variously used upper and lower case for the first letter of the first word after a colon (e.g. Note: This… and Note: this… ). For consistency and compliance with your style guide, you want to capitalize every initial letter immediately following a colon.

Solution

Run a macro to make the first letter after a colon a capital letter.

The critical parts of this find/replace macro are:

  • .Text = “: ([a-z])” — Word looks for a colon followed by a space, then a wildcard command for any lower case letter
  • .MatchWildcards = True — Word treats the .Text values in the parentheses as a wildcard string
  • Selection.Range.Case = wdUpperCase — When a lower case letter after a colon and a space is found, Word changes it to upper case.

Macro to capitalize the first letter after a colon

Sub CapsAfterColon()
    With Selection.Find
        .ClearFormatting
        .Text = ": ([a-z])"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
        While .Found
            Selection.Range.Case = wdUpperCase
            Selection.Collapse Direction:=wdCollapseEnd
            .Execute
        Wend
    End With
End Sub

Other options

If always you want the first letter after a colon and space to be in lower case, make these changes to this macro:

  • .Text = “: ([A-Z])” — Word looks for a colon followed by a space, then a wildcard command for any upper case letter
  • Selection.Range.Case = wdLowerCase — When an upper case letter after a colon and a space is found, Word changes it to lower case.

If a macro is too scary for you, you can partially run this as a Find/Replace action. Type : ([a-z]) in the Find field, click More and select Match Wildcards, then click Find Next to find the first instance. You can then change each found word’s initial letter to upper case, one at a time.

[Links last checked June 2011; this macro is a modification of one from Allen Wyatt’s Word VBA Guidebook (http://store.tips.net/T010353_Word_VBA_Guidebook_Table_of_Contents.html)]

4 comments

  1. Thanks, was looking for this.


  2. This is exactly what I need! But I get an error already at Selection.Find. How does Selection.Find work?


  3. Hi Hilde

    I think you have to select some text first before the macro will work — it needs something to run the ‘find’ on.

    –Rhonda


  4. I was looking for something almost exactly like this. Thanks a lot, and can confirm for readers that it works up through Word 2016.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: