Word Macro for Shift F3-Like Auto Capitalization That Skips the Words And, But, Or, For and To

Hi,

In Word you can select text, hit Shift+F3, and then scroll through a variety of Auto Capitalize scenarios: All Caps, All Lower Case, All Initial Caps.

Is there any way to write a Macro that would do the Initial Caps only AND always ignore the words

and, for, to, a, of

So that, selecting the text and running the Macro on

the history of search engines and consumer response

would become

The History of Search Engines and Consumer Response

rather than

The History Of Search Engines And Consumer Response

Thanks,

OT
oaktreesAsked:
Who is Participating?
 
GrahamSkanRetiredCommented:
This uses Find/Replace to keep listed words in lowercase
Option Explicit

Sub SetTitlecase()
    Dim strLowerWords() As String
    Dim strProperWords() As String
    Dim rng As Range
    Dim rngWord As Range
    Dim i As Integer
    
    'set up except words
    strLowerWords() = Split("and,for,to,a,of", ",")
    
    ReDim strProperWords(UBound(strLowerWords))
    For i = 0 To UBound(strLowerWords)
        strProperWords(i) = UCase(Left(strLowerWords(i), 1)) & LCase(Mid(strLowerWords(i), 2))
    Next i
    
    'set range to work on
    Set rng = Selection.Range
    
    With rng
        'capitalise first letter of every word
        .Case = wdTitleWord
        
        'set first letter of minor words to lower case
        For i = 0 To UBound(strLowerWords)
            With rng.Find
                .Text = strProperWords(i)
                .Replacement.Text = strLowerWords(i)
                .MatchWholeWord = True
                .MatchCase = True
                .Execute Replace:=wdReplaceAll
            End With
        Next i
    End With
    
    'recapitalise first letter of sentences
    rng.Case = wdTitleSentence
End Sub

Open in new window

0
 
oaktreesAuthor Commented:
Thanks a million!  Please keep an eye on this question.  Will try to get time to install this over the next few days.  VERY grateful! :)))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.