Detecting the Beginning of a Sentence in a Macro

by Allen Wyatt
(last updated June 29, 2021)


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
        Do While .Execute(FindText:="provided that", _
          Forward:=True, _
          Wrap:=wdFindStop) = True
            orng = "provided that"
            orng.Collapse wdCollapseEnd
    End With

    ' Account for end-of-sentence
    For i = 0 To UBound(vFindText)
        Set orng = ActiveDocument.Range
        With orng.Find
            Do While .Execute(FindText:=vFindText(i), _
              MatchCase:=True, _
              Forward:=True, _
              Wrap:=wdFindStop) = True
                orng = vReplaceText(i)
                orng.Collapse wdCollapseEnd
        End With
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
        .Wrap = wdFindContinue
        .Forward = True
        .Format = True
        .MatchCase = False
        .MatchWildcards = False
        .Text = "provided that"
        While .Found
            Selection.Range.Case = wdLowerCase
            Selection.Range.Case = wdTitleSentence
            Selection.Collapse Direction:=wdCollapseEnd
    End With
End Sub

Important! 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.


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, and Word in Microsoft 365.

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. ...


Recording a Macro

One of the most common ways of creating macros is to use Excel's macro recorder. This tip shows how easy it is to use the ...

Discover More

Printing Comments

Comments are a great way to share, well, comments with other people looking through your documents. If you want to print ...

Discover More

Selecting a Paper Source

If your printer allows you to specify different paper trays as sources for paper, you need to know how to select those ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Locating the My Documents Folder

Need to do some macro processing of documents in the user's My Documents folder? First step is to figure out where the ...

Discover More

Controlling the Hidden Text Attribute

Want your macro to change the Hidden attribute for some text in your document? It's easier to change than you might think.

Discover More

Displaying the User Name in the Status Bar or Title Bar

Sometimes it can come in handy to know who the current computer user is, as far as Word is concerned. This tip presents ...

Discover More

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

View most recent newsletter.


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 two less than 7?

2015-08-20 22:54:09

Ken Endacott

The Find and Replace settings to replace multiple spaces with a single space are:
Find what: [^32]{2,}
Replace with: ^32

You may also want to remove any spaces before a paragraph mark. The settings are:
Find what: [^32]{1,}^13
Replace with: ^p

2015-08-20 10:01:18


Von: Easiest way is to use Find and Replace. Simply search for a period followed by two spaces and replace it with a period followed by one space.

Repeat the same process for any other sentence-ending punctuation you may use in the document.


2015-08-20 08:47:58

Von Jones

How can someone delete two spaces between sentences in a document and replace them with one? I ask this question because I came from the old school where two spaces between sentences was required. I submitted a 85,000 word manuscript to an editor but my work was rejected because I used the two-space rule. It is a hard job to go through the entire document removing spaces.


Vondell (Von) Jones

2015-08-18 09:11:12

Ken Endacott

In Word VBA a sentence is theoretically a string of characters ending in one (or several contiguous) special punctuation characters question mark, exclamation mark, period or paragraph mark. The sentence includes the punctuation character and any trailing spaces. Thus text such as "Smith et al. authors" would be regarded as two sentences. But not if there is a comma following the period!

Word does strange things with sentences is if the punctuation is not straight forward. Things that upset it are ? or ! in the middle of a word for example peop!e, a word ending with ., such as etc., or a period inside a closing quotation mark for example people.". In these cases the string containing the offending characters is not a member of the Sentences collection and cannot be accessed with statements such as:
Strng = Activedocument.Sentences(n)

What this means is that the macros will work for most cases but there may be circumstances where there are some unintended results. Preferably clean up all punctuation anomalies before running the macro.

2015-08-17 10:59:39


The proposed macro solution does not address periods that appear in the middle of a sentence, which would include instances of abbreviations like Inc., Corp., etc., et al.

Note that the latter period here *is* at the end of a sentence (albeit a contrived one) even though it would otherwise be required for the abbreviation of "alii."

2015-08-15 06:13:23

Ken Endacott

Rather than test for sentence ending punctuation marks there is a much simpler way of determining if the selected text is at the start of a sentence:
If Selection.Start = Selection.Sentences(1).Start Then
,, ,, ,,
End If
This method will also work if there is more than one space between sentences.

One thing to note is that changing case whether by using SHIFT/F3 or with the VBA Case statements does not show as a revision in Track Changes. This is a shortcoming because it is usually desirable to show all editing changes in a document.

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.


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.