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

Delete partial sentences

Word • Macros • Editing
Peter Ronhovde
7
min read

Trim your sentences faster in Word. Delete to the end of a sentence using this VBA macro.

I hate to admit it, but this is probably my second most used macro, so you should definitely try it out.

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:

Sub DeleteSentenceToEnd()
  ' Delete to end of the current sentence

End Sub

As a quick reminder, the single quote on the second line tells the VBA editor that the rest of the text on that line is a only for human readers.

Delete to end of sentence

We previously used the Selection Expand command to prep deleting whole sentences.

  Selection.Expand Unit:=wdSentence

But that doesn’t work for deleting to the end of a sentence.

Deleting to the end of a sentence with a macro

We don't want to expand the Selection to the entire sentence before deleting it, so we need another approach.

Selecting to the end of a sentence

Using the EndOf command

We’ll instead extend the selection to the end of the sentence with a less-used move command.

  Selection.EndOf Unit:=wdSentence, Extend:=wdExtend

As with some other move commands covered before, we needed a unit of wdSentence.

The Extend parameter can be set to the default wdMove which collapses the Selection and just moves the insertion point, but we want wdExtend which extends the selection instead.

MoveRight command contrast

We could accomplish something similar using a regular move command instead:

  Selection.MoveRight Unit:=wdSentence, Extend:=wdExtend

But the two approaches are not quite the same.

The MoveRight command moves or extends to the next Unit regardless of the current position in the document, but the EndOf command doesn’t move the insertion point if it is already at the end of the current Unit specified or extend the Selection if the selection already ends there.

The latter behavior more closely matches our needs here.

Finish the macro

Once we have the end of the sentence selected, we just need to delete it.

  Selection.Delete

Don’t forget you could instead Cut the Selection to the clipboard or write a separate macro for that version if you prefer.

Putting the two commands together, the final simpler version of the macro is:

Sub DeleteSentence()
  ' Delete to the end of the current sentence

  ' Select to the end of the current sentence
  ' EndOf does not extend past the current sentence if already at the end.

  Selection.EndOf Unit:=wdSentence, Extend:=wdExtend
  Selection.Delete
End Sub

I assigned my version to Option+Shift+Delete on a Mac or Alt+Shift+Delete on Windows.

Variations

Omit ending punctuation marks

I prefer to delete the text of a sentence not the ending punctuation marks.

Removing all these ending punctuation marks increases the utility of the macro since it naturally handles dialog and parentheses.

With this in mind, after we’ve selected to the end of the sentence, we want to back up the End marker of the Selection. I create a character list first, so the move command reads easier.

  TrimCharacters = "?.!)] " + Chr(211) + Chr(34) + vbCr

Adding strings (of characters)

Notice how we add characters in a string using the + sign. This is called “concatenation,” but everything has to be a character or a string (of characters). We could also use the ampersand character, but they're not exactly the same.

Paragraph markers

vbCr is a paragraph marker (i.e., a return character) in Word. Including this character is definitely necessary since the initial extension of the selection will include any trailing paragraph marker(s).

While Word is smart enough in this case not to delete the trailing paragraph marker (sometimes the default Word behavior feels like it works against use when writing macros), it needs to be removed in this macro since we won’t be able to remove the other punctuation marks either.

End of sentence spaces

Note the space inside the double quotes which is important because Word considers the current sentence to extend all the way to the beginning of the next sentence. It sounds obvious, but that includes the space separating the sentences.

Double quotes

We want to naturally handle dialog quotes, but how do we tell VBA that?

We can specify a double quote as two double quotes inside another pair of double quotes ““”” in VBA (and that’s not confusing at all, right?), but I think it’s easier and clearer to just refer to the double quotes by number. Even that has a small issue though.

How we specify a right “curly” double quote is system dependent. On Macs use Chr(211), and on Windows systems use Chr(148). The Chr(34) is a straight double quote which is the same on both systems.

If you happen to want to also omit single quotes in your version, including a right single quote uses Chr(213) on a Mac and Chr(146) on Windows. Add Chr(39) for a straight single quote (we could just include it as "'" in the parentheses, but I think this is clearer) for both systems.

  TrimCharacters = "?.!)] " + Chr(211) + Chr(34) + Chr(213) + Chr(39) + vbCr

Excluding the punctuation marks

After all that explanation, we finally back up the End of the Selection.

  Selection.MoveEndWhile Cset:=TrimCharacters, Count:=wdBackward

MoveEndWhile does what the command name implies. It will keep moving the End as long as it keeps finding one of the characters specified in Cset.

Count:=wdBackward is a special case that tells the command to do just that. In general, you could specify a maximum number of characters to move past with a positive number moving forward in the document and a negative number moving backward.

We’ll put these extra commands together in the final macros below.

Delete to Start of Sentence

The parallel macro to delete to the start of the sentence is nearly the same.

New trim characters

We don’t have ending punctuation marks, so we can reduce the number of trimmed characters.

  TrimCharacters = "[(" + Chr(210) + Chr(34) + vbCr

As mentioned previously, the “curly” double quote characters are different on Windows and Mac systems. Specifically, the left double quote character in Word for Mac is given by Chr(210) but Chr(147) on Windows. The straight double quote is still Chr(34) everywhere (that uses the ASCII numbering scheme).

If you want to exclude single quote characters, the left single quote is Chr(210) in Word for Mac but Chr(147) on Windows. The straight single quote Chr(39) works on both systems.

  Selection.MoveStartWhile Cset:=TrimCharacters

As with MoveEndWhile in the other version, MoveStartWhile keeps moving the Start of the Selection forward in the document as long as it keeps finding one of the characters specified in Cset.

If you want to limit how many characters you move past, give a Count parameter with a positive number (see above).

Capitalize the new sentence

After we’ve deleted to the beginning, you’ll find yourself manually capitalizing the new first word often. It won't take long to get annoyed with this, so we should make VBA do it for us.

This is handled by the Case property of a Range (essentially a chunk of contiguous content in the document with a defined Start and End; more on them coming later).

  Selection.Range.Case = wdTitleSentence

Note how we specify the Range of the Selection before specifying the Case property. This must be done since the Selection doesn’t have its own Case property.

In common help-page fashion, the descriptions in the list of VBA case constants are terse (details matter when writing macros), but in short, the constant wdTitleSentence is probably best suited here, but it doesn’t quite act like we would expect in all cases.

It’s actually an aside, but I want to clarify how the wdTitleSentence Case constant works. In particular, the Case property/command seems to understand when it’s inside a sentence when using wdTitleSentence even when the entire sentence isn’t selected.

This is okay in our current macro since we are confident our selection includes the beginning of the sentence (it won’t capitalize the first word otherwise), but in other macros, I will sometimes just collapse the Selection near the relevant word and use wdTitleWord instead just to be sure a word is capitalized as I intended.

Using the constant wdTitleWord makes the Case property/command capitalize the first letter of each word in a Range.

Final macros

Now we get to the final macros. Hopefully, you didn’t skip all the way to here. There are several useful points above if you want to write more macros.

Deleting to the end of a sentence:

Sub DeleteSentence()
  ' Delete to the end of the current sentence

  ' Select to the end of the current sentence
  ' EndOf does not extend past the current sentence if already
  ' at the end.

  Selection.EndOf Unit:=wdSentence, Extend:=wdExtend

  ' Trim most sentence ending punctuation marks
  TrimCharacters = "?.!)] " + Chr(211) + Chr(34) + vbCr
  Selection.MoveEndWhile Cset:=TrimCharacters, Count:=wdBackward

  ' Delete the trimmed selection
  Selection.Delete
End Sub

I assigned my implementation to Option+Delete on a Mac or Alt+Delete on Windows.

Deleting to the start of a sentence:

Sub DeleteSentenceToStart()
  ' Delete to the start of the current sentence

  ' Select to the start of the current sentence
  ' StartOf does not extend before the current sentence if already
  ' at the beginning.

  Selection.StartOf Unit:=wdSentence, Extend:=wdExtend

  ' Trim initial parentheses or double quotes
  TrimCharacters = "[(" + Chr(210) + Chr(34)
  Selection.MoveStartWhile Cset:=TrimCharacters

  ' Delete the trimmed selection
  Selection.Delete
End Sub

I assign this macro to Control+Option+Delete in Word for Mac or Alt+Backspace on Windows.

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.