1

For converting selected text in Microsoft Word to title case, I found the following useful macro at: keyboard shortcut for title case in word 2010

I'm wondering if there is a way to modify the macro so that it always capitalizes a word the follows a colon, even if the word is on the "exceptions" list (e.g., and, of, in, the).

Sub TitleCase()
Dim lclist As String
Dim wrd As Integer
Dim sTest As String

' list of lowercase words, surrounded by spaces
lclist = " of the by to is from a and but as at in "

Selection.Range.Case = wdTitleWord

For wrd = 2 To Selection.Range.Words.Count
    sTest = Trim(Selection.Range.Words(wrd))
    sTest = " " & LCase(sTest) & " "
    If InStr(lclist, sTest) Then
        Selection.Range.Words(wrd).Case = wdLowerCase
    End If
Next wrd
End Sub

For example, if the following text were selected, "this is my title: the subtitle goes here", the result would be: "This Is My Title: The Subtitle Goes Here", even though "the" is on the exceptions list.

2

1 Answer 1

1

I revised your code so it would ignore the word after a colon.

Sub TitleCase()
Dim lclist As String
Dim wrd As Integer
Dim sTest As String
Dim skipNext As Boolean

' list of lowercase words, surrounded by spaces
lclist = " of the by to is from a and but as at in "

Selection.Range.Case = wdTitleWord

For wrd = 2 To Selection.Range.words.Count
    sTest = Trim(Selection.Range.words(wrd))
    Select Case sTest
        Case ":"
            skipNext = True
        Case Else
            If skipNext = True Then
                skipNext = False
            Else
                sTest = " " & LCase(sTest) & " "
                If InStr(lclist, sTest) Then
                    Selection.Range.words(wrd).Case = wdLowerCase
                End If
            End If
    End Select
Next wrd
End Sub
1
  • I can't thank you enough, Rich! This will be hugely helpful in my work. Much appreciated.
    – Greg
    Jan 21, 2021 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.