Heading

This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
min read

Capitalize a sentence

Word • Macros • Editing
Peter Ronhovde
10
min read

Some trivial editing tasks are tedious but necessary. Capitalizing the first word of a sentence is one such task. We create a macro to correct the sentence capitalization from anywhere within the sentence, so we can just keep working.

Thanks for your interest

This content is part of a paid plan.

Capitalize the current sentence

We will capitalize the first word of the current sentence without having to do it manually—

Really? That’s what the keyboard and mouse are for, right?

Yes, but—

You’re just finding things for us to do!

Not hardly, but I admit I have an ulterior motive today … we need to reduce all the clickity-clackity around and get back to the human part of the editing process as fast as possible. Let the pack mule (computer) do what it does best, and we can do what we do best. (No, that's not the ulterior motive yet, so just hold your horses.)

Why?

Is this what we’ve devolved to? Capitalizing words? Has he run out of ideas?

I’ve got more cargo in the wagon bed, but this one is simple and yet practical its own almost trivial way. It also introduces using Ranges and learning to think more like VBA without a field of tumbleweeds blowing in the wind distracting us with all the additional steps and considerations.

Maybe it's not the most productive macro in the editing world by itself, but even so, it’s nice to avoid manually jumping to the start of a sentence to capitalize a single letter before moving back to where we were working.

What are the manual steps?

What steps are required to manually capitalize the first word of the current sentence?

  1. Use the mouse (slow) or keyboard to move to the beginning of the sentence
  2. Delete the first character
  3. Type the new capitalized first character

It's not as if we haven't done this a few thousand times, but we could walk to work rather than ride a horse (or a car for the high falutin among us). Ain't many folks volunteering for that, I reckon.

Word has a standard keyboard shortcut Shift+F3 to cycle the capitalization of a Word (or text in a selection), but we still need to move to the beginning of the sentence. We could improve our sentence navigation shortcuts which would help speed things up in step 1. No standard shortcuts exist in Word to navigate between sentences, but we assigned several shortcuts in a previous article to some hidden Word commands to make sentence navigation easier.

We could do literally mimic the manual steps in VBA, but that’s clumsy and unnecessary. Instead, we should to think a little more like VBA when creating our macro.

Talk about VBA thingies

VBA represents various document elements as virtual "objects". Each object has related data (called "properties) and actions (called "methods") that allow us to control it and the associated document content.

What is the Selection?

The Selection (with a capital "S") is VBAs representation of the visible selection or insertion point on the screen. We can move it around, collapse it, delete the text, etc. Since it controls the actual selection on screen, any changes are immediately visible, but only one Selection exists per document. The Selection is like a specialized Range with some extra properties and methods. See our brief introduction to the Selection for more information.

Related to the current macro, the Selection contains several collections (essentially fancy VBA lists) of partially or fully spanned document elements. The content could include Words, Sentences, Paragraphs, Bookmarks, etc. We will use the Sentences collection of the Selection for this macro.

What is a Range?

Conceptually, a range corresponds to a contiguous region of document content. VBA includes a generalization of this concept called a Range (with a capital "R"). It is tracked literally using Start and End character positions in the document, but the VBA representation also includes other properties and methods that allow us to control its extent, modify text, control capitalization, and more. See our brief introduction to Ranges in Word VBA for more information.

Many document elements are treated as ranges such as sentences, words, characters, headings and their content, etc. Like the Selection, a valid range contains collections with essentially the same kinds of (perhaps partially) spanned document elements as above.

Create the empty macro

A previous post covers creating an empty macro like the one shown below using the application interface. Afterward, we’ll have a macro skeleton something like:

Sub CapitalizeCurrentSentence()
' Capitalize first word of the current sentence

End Sub

The single quote tells VBA the rest of the text on that line is a comment meant for human readers. We start our macro steps on the empty line, but we literally have a one-line macro in this article.

Capitalize the first word of the sentence

For the capitalization, we need the Case property of a Range to change the capitalization, so we need to identify the current sentence range.

What are the macro steps?

We could design the macro to naively by following the manual steps itemize above, but the best macros will often not align with them because VBA works differently. The differences can twist our brains a little until we get used to the VBA concepts, but overall, it’s more of an advantage than a detriment. VBA sees the document internals, and the commands can do things our fingers can’t quite replicate directly without a lot more effort, if at all.

In VBA terms, steps are not quite as obvious, so let's outline them.

  1. Get the first sentence range
  2. Capitalize the first word of the sentence range

These steps work the same when used from anywhere within the sentence because we do not modify the current Selection.

Get the first sentence Range

We refer to the Sentences collection of the Selection to access the current sentence range.

Selection.Sentences ' Not done ...

We need the range of the first sentence, so we use the collection's First property.

Selection.Sentences.First ' Still not done ...

The Sentences collection stores the ranges of the sentences (unlike Paragraphs or some other collections which store objects of that specific type), so the First property refers to the first sentence range. If more than one sentence is spanned by an initial selection, it only uses the first one. This reference also works correctly even if the Selection is just an insertion point or does not span the full sentence.

Change the case

Every valid range has a Case property that can change the case of the included word(s), so we use the first sentence range from above and refer to its Case property.

Selection.Sentences.First.Case ' Almost there ...

Apply the sentence title case to the Selection's Range to capitalize the first word:

Selection.Range.Case = wdTitleSentence

We assigned the wdTitleSentence constant to the Case property using an equals "=" sign since the property is stored as a numerical value. Other Case constants exist, but the sentence title value works as expected in most cases, automatically capitalizing the first word of the sentence. The wdTitleWord case constant would not work in this instance since it would capitalize every word in the sentence more like a document title, not just the first word.

Are we done?

Since we're referring to the first sentence range rather than directly manipulating the Selection, we do not need to take any other action after the capitalization. We're (mostly) done.

Any gotchas?

What unforeseen issues could exist with the macro?

Starting selections?

A typical consideration for most text-oriented macros is whether a starting selection exists when the user runs the macro. Does it matter here?

We’re not deleting any text or reorganizing content. We're using the current Selection to find its first sentence range. We're not modifying the extent of the Selection in any way, so an initial selection of spanned content can't cause a problem.

Mid-sentence capitalization issues

Since we're using wdTitleSentence, the case is applies across the entire sentence range. This sounds good, and it usually is since the property (kind of) understands the sentence structure and many word capitalization expectations … but we've got a small problem, especially for the perfectionists out there.

Some words have a different standard capitalizations such as USA or a possible variable name FirstCharacter. Word understands USA and will not change the abbreviation, but it does not know the FirstSentence name. Its capitalization will be changed to a regular-ish word firstsentence.

Ughhh.

It's not the end of the world, since these types of words will not occur in many novels, but if you do and run the macro such as sentence, the unintended case changes for those words would be annoying and counterproductive. Moreover, updating the proper capitalization information for new words seems to be more involved than just adding the word to an accessible Word dictionary. The member version solves this mis-capitalization issue for all but the first word of the sentence.

Finish the macro

We literally need just one (somewhat long) command for this macro.

Sub CapitalizeCurrentSentence()
' Apply sentence capitalization to the current sentence
Selection.Sentences.First.Case = wdTitleSentence
End Sub

I assigned my variant of this macro to Option+F3 in Word for Mac or Alt+F3 on Windows mostly because the standard Word keyboard shortcut to change the case of the current word (or words in a selection) is Shift+F3. I actually don't like this shortcut since it is a little awkward to tap quickly. However, it is easy to remember, and I haven't found a better one yet.

This macro is only saving two steps, but those little steps add up over time, and it’s just nice not to have to do them manually. The member version further avoids any mid-sentence mis-capitalization issues by referring only to the first word of the sentence. It's a little extra VBAish, but it's still nice and compact. It further accounts for double quotes in dialog as well as parentheses.

Scruffy alternative

Skip this version, if you're not interested in any details about what works and what doesn't.

Why mention an alternative if we don't use it?

It's sometimes instructive to see a poorer solution. The version below is still thinking like VBA to some extent (rather than just a recording a macro using the manual steps). While it's still functional, it has an undesirable side effect.

More direct approach (but still VBAish)

Without explanation, a functionally similar macro to the first one above could manipulate the Selection directly to accomplish the same overall task.

Sub CapitalizeCurrentSentence()
' Apply capitalization to the current sentence
Selection.Expand Unit:=wdSentence
Selection.Collapse ' Collapse to beginning of sentence
Selection.Range.Case = wdTitleSentence ' Just the first word
End Sub

It's direct and easy to follow. The collapse step takes advantage of the Case property applying to the current word if the range is empty.

Jumping cursors! (Side effect)

This version moves the insertion point to the beginning of the sentence while adjusting the capitalization. It’s not a major problem, but it is annoying since half the point of the macro is to make the change and keep working otherwise uninterrupted.

We could add some steps to track and restore the initial starting position or selection. However, the extra effort is unnecessary when better solutions exist.

Affiliate Links

If you're interested in using Word or another tool related to the article, check out these affiliate links. I may make a small commission if you purchase when using them, but there is no increase in cost for you, and it helps to support this site and associated content.

I've been using Microsoft for Business for commercial use (that's us writers) on one of the lower pricing tiers for years. I get to use my macros, have online storage, and don't have to worry about software updates.