Detecting the Beginning of a Sentence in a Macro

Written by Allen Wyatt (last updated September 3, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


1

Shelley is developing a macro that changes all capitalization permutations of "provided that" ("Provided That", "PROVIDED THAT", etc.) to lowercase. That is easy enough to do, but if the phrase starts a sentence the "P" should be capitalized. Shelley wonders if there is a way, in a macro, to tell if a phrase such as "provided that" is at the beginning of a sentence.

There are a couple of ways you could go about this. First, you could use Find and Replace to do successive searches and replacements. The macro could do the following:

  1. Change all variations of "provided that" to lowercase.
  2. Search for ". provided that" (period, space, "provided that") and replace it with ". Provided that" (proper capitalization).
  3. Repeat the search and replace in step 2 using other sentence-ending punctuation marks such as an exclamation mark, a question mark, or any of those punctuation marks followed by a quote mark.

Since this is being done in a macro, the process should go very quickly. Here's an example of one that could do the job:

Sub ChangeCase1()
    Dim vFindText As Variant
    Dim vReplaceText As Variant
    Dim orng As Range
    Dim i As Long

    vFindText = Array(". provided that", _
                      "! provided that", _
                      "? provided that", _
                      "."" provided that", _
                      "!"" provided that", _
                      "?"" provided that")
    vReplaceText = Array(". Provided that", _
                         "! Provided that", _
                         "? Provided that", _
                         "."" Provided that", _
                         "!"" Provided that", _
                         "?"" Provided that")

    ' Set all occurrences to lowercase
    Set orng = ActiveDocument.Range
    With orng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        Do While .Execute(FindText:="provided that", _
          Forward:=True, _
          Wrap:=wdFindStop) = True
            orng.Select
            orng = "provided that"
            orng.Collapse wdCollapseEnd
        Loop
    End With

    ' Account for end-of-sentence
    For i = 0 To UBound(vFindText)
        Set orng = ActiveDocument.Range
        With orng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            Do While .Execute(FindText:=vFindText(i), _
              MatchCase:=True, _
              Forward:=True, _
              Wrap:=wdFindStop) = True
                orng.Select
                orng = vReplaceText(i)
                orng.Collapse wdCollapseEnd
            Loop
        End With
    Next
End Sub

One shortcoming to this approach is that it doesn't allow for "provided that" occurring at the beginning of a paragraph. The macro could be modified to make it take such instances into account, but it would be more complex.

There is a different approach you can use, however. This one relies on a rather esoteric setting for the Case property: wdTitleSentence. This setting changes all the characters (with one notable exception) to lowercase and if the phrase is at the beginning of a sentence will change the first character to uppercase. Here's how you could use it in a macro:

Sub ChangeCase2()
    With Selection.Find
        .ClearFormatting
        .Wrap = wdFindContinue
        .Forward = True
        .Format = True
        .MatchCase = False
        .MatchWildcards = False
        .Text = "provided that"
        .Execute
        While .Found
            Selection.Range.Case = wdLowerCase
            Selection.Range.Case = wdTitleSentence
            Selection.Collapse Direction:=wdCollapseEnd
            .Execute
        Wend
    End With
End Sub

Note that within the While loop there are two assignments made to the Case property. The first sets the selection (what was found) to lowercase and the second sets it to title case for a sentence. The lowercase setting is made specific to handle the "one notable exception" I mentioned—if the text "PROVIDED that" is at the beginning of a sentence, the wdTitleSentence setting will not, for some reason, change it to "Provided that." It will, however, change "provided that" (which "PROVIDED that" becomes after setting it to lowercase) to "Provided that" with no problem.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13398) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Controlling Endnote Placement

Endnotes are often used in technical and scholarly documents. You can control exactly where the endnotes appear in your ...

Discover More

Counting Characters in Text Boxes

When you do a word count, the value that is returned does not include any text contained in text boxes. This may not be a ...

Discover More

Referencing the Last Cell in a Column

When developing formulas, you may need to reference the very last value in a particular column. This can seem perplexing, ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Attaching Macros to Documents

When you distribute documents to other people, you may want those documents to have associated macros that the reader can ...

Discover More

Editing Word's Built-in Commands

Want to configure Word to do just what you want it to? You can even go so far as to change the actual way in which Word ...

Discover More

Automatically Inserting Brackets

Want a fast way to add brackets around a selected word? You can use this simple macro to add both brackets in a single step.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 + 5?

2022-09-06 10:24:30

Andrew

Why not just add "^pprovided that" to the end of the vFindText array and "^pProvided that" to the end of vReplaceText?

Andy.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.