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 sentence

Word • Macros • Editing
Peter Ronhovde
15
min read

Capitalize 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 …

Thanks for your interest

This content is part of a paid plan.

Create the empty macro

A previous post covers creating an empty macro like the one shown below. When you’re done, you’ll have 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.

Why?

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

No, I’ve got a lot more cargo in the hold; but this one is simple, practical its own way, and it lets us introduce using Ranges in the extended content below without a lot of other flotsam floating around to distract us with additional steps and considerations.

So maybe not the most productive macro in the world by itself, but even so, it’s nice not to have to manually jump to the start of a sentence to capitalize it and then move back to where I was working.

What are the manual steps?

What steps does it require to capitalize the first word of the current sentence manually?

I real life, we would:

  • Use the keyboard or mouse to move to the beginning of the sentence (slower than you think if you use the mouse)
  • Delete and then replace the first character

No standard keyboard shortcut exists to navigate between or select sentences in Word, but see the previous article where we assigned keyboard shortcuts to several default Word commands to make navigating between sentences easier.

We could do these steps literally in today’s macro, but that’s clumsy and unnecessary.

We need to think a little like Word instead.

What are the macro steps?

The macro steps don’t quite align with the manual steps because VBA works a little differently.

The differences can twist your brain a little until you get used to the concepts, but overall it’s more of an advantage than a detriment since the VBA commands can do things our fingers can’t quite replicate directly without a lot more effort.

So what’s different?

For the capitalization, we instead use the Case property of Ranges to make the change. We’ve done something similar in a previous macro where we deleted to the beginning of a sentence.

We first need to select the sentence Range, so we can apply the correct Case property.

Macro steps

Let’s list the steps for clarity since they’re not quite as obvious as the manual approach.

  • Expand the Selection to include the whole sentence
  • Set the Selection's Case property to sentence title case
  • Collapse the Selection

These steps work the same when used from anywhere within the sentence.

Now let’s work on the details.

Get the sentence Range

We once again use the Selection’s Expand command.

  Selection.Expand Unit:=wdSentence

The Unit constants include most of the common ones we think of when creating documents (e.g., words, sentences, paragraphs, etc.). This one uses Word’s standard sentence constant.

Change the case

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

  Selection.Range.Case = wdTitleSentence

Other Case constants exist, but this title sentence value works as expected in most cases, automatically capitalizing the first word of the sentence.

Notice we’re assigning the Case property as equal "=" to the title sentence constant since it happens to be an actual value for the Range.

Collapse the selection

Finally collapse the Selection since the user probably didn’t intend to select the whole sentence when running the macro.

  Selection.Collapse

It makes the most sense to collapse toward the Start of the Selection, which is the default, to finish the macro with the insertion point at the beginning of the sentence.

Finish the macro

Now put the commands together.

Sub CapitalizeCurrentSentence()
  ' Capitalize first word of the current sentence
  Selection.Expand Unit:=wdSentence
  Selection.Range.Case = wdTitleSentence
  Selection.Collapse
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, but I haven't found a better one yet.

This macro is “only" saving three steps, but those little steps add up over time, and it’s just nice not to have to do them manually. The version below is even a little better since it doesn't even move the insertion point when you run the macro.

Any gotchas?

Starting selections?

A typical gotcha consideration for most text-oriented macros is whether there is a starting selection when the user runs the macro.

Does it matter here?

We’re not deleting any text or reorganizing content, so this can't cause much of a problem today. By a "problem" for a writer, I mean messing up my content or formatting somehow.

The only side effects are the cursor moves and the user loses whatever starting selection they had. The extended content version below avoids even these small side effects by using a Range instead of the Selection.

Jumping cursors!

I find it a little jarring that this macro jerks the insertion point to the beginning of the sentence “without my permission.”

I didn’t run it with the intention of also moving the insertion point. It’s not a major problem, but it is not preferred (for me at least).

We could solve this issue by storing the current insertion point location and restoring it after we’re done.

It’s a little clumsy but workable, and I’ve done that occasionally, but Ranges solve both problems nicely allowing us to just keep typing or editing without interruption.

See the extended content below for a slightly revised macro that leaves our insertion point in place when we run the macro.

Mid-sentence capitalizations?

A few capitalized mid-sentence words may be changed.

It seems to happen when the word has a capitalized letter on the inside. It was even a little hard to pin down the exact conditions, so I would place this closer to the “annoyance” category. The above macro doesn’t account for it, but the Range version below solves the issue.

Reworking with Ranges

The macro steps are almost the same as before. We just have to add a step to define our initial Range before we reframe the commands in the context of the Range properties and methods.

Another article gives a brief introduction to Ranges if you're interested.

Get the Selection Range first

The new step is getting the current Selection Range, so we can work with it. We set is as a new Range like so:

  Set MyRange = Selection.Range

Notice I "Set" MyRange equal to the Selection Range because a Range is more than just a value in Word VBA. It contains other properties that we need to copy to the new Range variable.

I often use just “r” as an abbreviation for a working Range in my own macros since it’s just faster to type, but I picked something a little more descriptive for you.

We set MyRange given the starting Range of the Selection. This means MyRange has the exact Start and End positions of the Selection, whether just a plain insertion point or an actual span of selected text.

There is a hidden subtlety here regarding setting ranges equal to each other, but that is a lesson for another day.

Get the sentence Range

We use theRange’s Expand method to span the current sentence just like we did with the Selection in earlier macros.

  MyRange.Expand Unit:=wdSentence

Most of the same Unit constants apply (e.g., words, sentences, etc.) with the exception that we can’t expand by a line using wdLine like the Selection is able to do.

Change the case

Now apply the sentence title case to the sentence Range to capitalize the first word. Since MyRange is already a Range, we just reassign the Case property directly:

  MyRange.Case = wdTitleSentence

All the Word VBA case constants still work the same since we were actually accessing the Selection’s Range previously anyhow.

Collapse the Range?

In the original macro, we added a step where we collapsed the Selection at the end, so the user wouldn’t finish the macro with the sentence mysteriously selected when that wasn’t the intended action of the macro.

This step is no longer necessary because we never changed the user’s original selection or insertion point position. We only modified MyRange (but see the gotchas below).

The modified MyRange variable just disappears when the macro ends which doesn’t matter because we don’t need it any more.

Solving another gotcha

Nothing major, but we mentioned above if you test the macro, it sometimes converts words with a capitalized letter on the inside to lower case.

We can avoid this strange, unintended behavior by collapsing MyRange toward its beginning before applying the Case.

  MyRange.Collapse  ‘ avoid changing other words

Then we finish as before.

Finish the macro

Here is the revised macro using Ranges. Notice it almost runs exactly parallel to the original version above using the Selection.

Sub CapitalizeCurrentSentence()
  ' Capitalize first word of the current sentence
  Set MyRange = Selection.Range
  MyRange.Expand Unit:=wdSentence
  MyRange.Collapse  ‘ avoid changing other words
  MyRange.Case = wdTitleSentence
End Sub

This version is a little nicer because it doesn’t move or change the user’s current selection when we run the macro. We can just keep on typing or editing.

Further Improvements

As a writer, I like my macros to naturally account and work even when I'm editing dialog.

Skip double quotes and parentheses

Even the Range-based macro above doesn’t account for double quotes or parentheses, but we’ve handled that previously (see the previous article for a more detailed description).

As a writer, allowing for double quotes is an important addition. Skipping an open parenthesis is an easily added bonus while we’re already solving the problem.

Trimming the starting punctuation marks

Essentially, we have to trim those characters from the start of the selection.

To trim a character from the Start of a Range use MoveStartWhile like we did in another macro with the Selection.

Unfortunately for our macros, a left double quote character is different on Mac and Windows systems.

On a Mac use:

  MyRange.MoveStartWhile Cset:="[(" + Chr(210) + Chr(34)

In For Word for Windows use instead:

  MyRange.MoveStartWhile Cset:="[(" + Chr(147) + Chr(34)

Notice we start with "[(" which makes the command skip left parentheses and square brackets in addition to both forms of double quotes.

Fixing a new small gotcha

But this step introduces a subtle gotcha. The capitalization no longer works if we actually use it with dialog text!

Why?

The Case wdTitleSentence now recognizes that after removing the punctuation mark from the sentence Range, the start of the Range is no longer the actual beginning of the sentence, so the Case assignment won't to change the case of the first word.

We are already collapsing the Range before changing the case of the first word, so to remedy this issue, we can just change the Case constant to wdTitleWord instead.

  MyRange.Case = wdTitleWord

It will now capitalize the word immediately at the collapsed Range position without affecting the rest of the sentence.

Subtle gotchas comment

Notice how the issues are often not difficult to solve, but they can be subtle. You have to understand how Word is interpreting the commands and how it acts on the text.

That’s why I’m here. :-)

Cycle capitalization?

We could cycle the capitalization of the first word of the sentence similar to how the standard Word keyboard shortcut Shift+F3 acts for individual words, but I almost never want to do that.

If that sounds interesting to you though, change the Case constant assignment to wdNextCase.

  MyRange.Case = wdNextCase

Finish the macro redux

Here is the revised macro using Ranges while also allowing for dialog or parentheses.

Sub CapitalizeCurrentSentence()
  ' Capitalize first word of the current sentence
  Set MyRange = Selection.Range
  MyRange.Expand Unit:=wdSentence
  ‘ Remove double quotes or parentheses from beginning of the sentence
  MyRange.MoveStartWhile Cset:="[(" + Chr(210) + Chr(34)
  MyRange.Collapse  ‘ avoid changing other words
  MyRange.Case = wdTitleWord
End Sub

I assigned this macro to Option+F3 in Word for Mac or Alt+F3 on Windows.

Swap out Chr(210) for Chr(147) in Word for Windows, so skipping a left double quote works correctly.

Again, this version is a little nicer because it doesn’t move or change the user’s current selection when we run the macro, and it now works on dialog also. We can run the macro and just keep on typing or editing.

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.