by Allen Wyatt
(last updated August 15, 2015)
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:
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.
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, and 2013.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
If your macro needs to communicate with a user, one simple way to do it is to use a message box. Here's how to use this ...Discover More
Macros are often used to process documents. If part of the processing involves making text selections uppercase, Word ...Discover More
Sometimes it is necessary to change the gender of various pronouns used in a document. If you have a stock document that ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.