Trimming sentences faster in Word will obviously speed up the editing process for a manuscript. Toward this end, we create a pair of macros to delete to the beginning or ending of a sentence.
Thanks for your interest
This content is part of a paid plan.
Delete part of a sentence
Word includes some standard keyboard shortcuts for manipulating words or paragraphs, but sentences are also a rather fundamental writing unit. Word includes some essentially hidden commands for manipulating them, but the list is rather limited. Creating some extra macros to improve sentence editing efficiency is a worthy endeavor. Toward this goal, we'll create several macros that delete to the beginning or ending of a sentence.
While I don't like admitting it, my versions of these macros are probably some of my more used one, so you should definitely try it out. The base macro is short, so we take a couple side trails to enhance our VBA knowledge along the way. The member versions further enhance the base macros to account for quotes, improve handling end of sentence punctuation, as well as a few other details.
Create the empty macro
A previous post covers creating an empty macro like the one shown below:
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.
We'll start our commands on the blank line. After we've created the macro to delete to the end of a sentence, we'll easily modify it to delete to the start of a sentence.
Delete to the end of a sentence
We previously used the Selection Expand method to prepare for deleting a full sentence from anywhere inside it.
But we don't want to expand the selection over the entire sentence before deleting it, so we need another approach …
Picking the correct method
Part of writing good Word macros is picking the correct command for the task. Early on, I floundered, but I eventually discovered Word VBA already had some nicely targeted commands we can leverage for our specific needs. It's easy to overlook some of them, but if we'll just dig a little ... (sorry about the mixed metaphors but I'm trying to teach you how to fly not flounder).
Select to the end of a sentence
We need a way to just select part of a sentence. We could use standard move command (see MoveRight comments below) that has an extend option, but we can do even better for this macro.
Use the EndOf command
We’ll leverage a lesser used move method of the Selection called EndOf to setup our delete step later.
Given the preposition ending the method name, we expect to specify a relevant document unit. Word VBA understands several standard ones which are defined in the WdUnits constants table. We assign the desired constant using the Unit option with a colon equals := symbol.
This command uses Words exists algorithms to determine the end of a sentence. The selection will include the ending punctuation as well as any spaces up the beginning of the next sentence or even the paragraph if it is the last sentence of a paragraph.
Two advantages of using this method are:
- We can take advantage of Word's algorithms to detect sentences.
- It will not move if the End of the selection is already at the correct location.
Of course, in this macro, we'll use wdSentence, but the concept could easily be extended to another document unit types (probably paragraphs).
The Extend parameter requires a constant from yet another short WdMovementType constants table. It defaults to wdMove which collapses the Selection and just moves the insertion point, but we want wdExtend which instead extends the selection from the End position leaving the Start position unchanged.
Contrast with the MoveRight method
Taking an announced teaching side trail, we could accomplish something similar using the regular MoveRight method instead. Without going into a lot of detail, the command would be:
It looks very similar, and it even has an Extend option to extend the selection not just move the insertion point. Yay!
What's all the fuss?
The two approaches are not quite the same. The MoveRight command moves or extends to the next Unit regardless of the current position or context in the document, but the EndOf command doesn’t move the insertion point or extend the selection if it is already positioned at the end of the specified Unit.
Using MoveRight this way would not be wrong per se, but we would need to add one or more extra steps or conditions to account for the special case. Why bother when EndOf does exactly what we need?
Select up to the end of sentence punctuation?
Depending on how you prefer your macro to function, you might instead want to jump to end of sentence punctuation. It feels more natural (to me) to delete the sentence text up to but not including the period (or whatever). The correct command would be the MoveEndUntil method.
This command moves the End position of the Selection until it finds any character specified in a character set Cset. For this macro, we're looking for typical sentence ending punctuation such as a period ".", a question mark "?", or an exclamation point "!". With these, our character set is, Cset := ".?!".
We could encounter problems at the end of a paragraph, so it's probably safer to include the paragraph marker character vbCr from another VBA constants table. We need to add the vbCr character to the Cset option. For that, we use a plus + sign to "concatenate" the two strings of characters, Cset := ".?!" + vbCr.
Our command to handle end of sentence punctuation is then:
This command would replace the previous EndOf command to find the end of the sentence.
Why is this better?
It more intuitively deletes the sentence up to but not including the end of sentence punctuation mark, but it is obviously a preference.
Where might it fall short?
The MoveEndUntil command is wonderful at first glance ... until we realize it's kind of dumb. It will not automatically detect and step over a decimal number or an abbreviation like etc. See the member version for an improvement more likely to handle ambiguous sentence punctuation.
Delete the partial sentence
We've seen the Delete method before, but for completeness it is simply:
It's almost anticlimactic at times to finish the macro. Although, the suggested improvements at the end raises a question about possible empty selections.
Delete with a sentence Unit?
On another teaching aside, if you've used Command+Delete (Mac) or Control+Delete (Windows) in Word to delete words, you might remember it also deletes partial words if the insertion point is already in the middle of a word.
Well … the Delete method actually has a Unit parameter also. Couldn't we just use a wdSentence Unit with it and mimic that delete behavior with words?
Uhhh … sounds good at first, but nope.
We can't use a sentence unit with the Delete method because it just does not understand what to do with a sentence like it does with a word. That job falls to us.
Delete to start of sentence
The sibling command to delete to the start of a sentence is just:
It has the same behavior and options as EndOf. It relies on Word to properly detect the beginning of the sentence, and it will not move if the Start position of the selection if it is already at the beginning of the sentence.
Capitalize the new first word
After we’ve deleted to the beginning of a sentence, we’ll need to manually capitalize the new first word most of the time. It won't take long to get annoyed with this, so we should make VBA do it for us.
This is done with the Case property of a Range (essentially a chunk of contiguous content in the document with a defined Start and End; see our introductory article for more information).
We specified the Range of the Selection before Case because 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). Briefly, the constants wdTitleSentence or wdTitleWord are probably best suited for our purposes most of the time, but they may not act exactly like we expect.
The wdTitleSentence case will only capitalize the first word of a sentence, and it seems to understand when it’s inside a sentence even when the entire sentence isn’t selected. This works well with our current macro since we are confident our selection resides at the beginning of a sentence. The fact that we actually have an insertion point with no text selected won't affect the first word capitalization. We'll go with wdTitleSentence just to keep things simple.
The disadvantage of including the automatic capitalization is Word does not understand other special words like LastCharacter. It will convert the uppercase "C" in the middle of the word to a lowercase "c". That occurs much less often, so it's a reasonable compromise.
Final delete partial sentence macros
Piecing the commands together, the final versions are:
Delete to sentence end
The first version uses Word's sentence finding algorithm to let it pick the end of the sentence, but it does not stop at the end of sentence punctuation.
If you prefer the punctuation-based version (like I do), just change EndOf to an appropriate MoveEndUntil method where we specify the appropriate sentence ending punctuation.
The downside to this version is it doesn't actually understand sentence structure like the EndOf method does, so abbreviations or decimal numbers may mess it up. See the member version which attempts to solve this issue.
I assigned my version to Option+Control+Shift+Delete on a Mac or Alt+Shift+Delete on Windows. The Mac version needs to include the Control modifier key. On a Mac, the DeleteToSentenceStart macro more naturally uses Option+Shift+Delete to delete backward in the document to be consistent with how Macs view the Delete key as a backward delete action. MacOS has limited Backspace key support when using modifier keys.
Delete to sentence start
The start version trivially swaps out StartOf for EndOf, but the differences are a little bigger in the extended version.
I assigned my version to Option+Shift+Delete on a Mac or Alt+Shift+Backspace on Windows.
Improvements or extensions
What could we do to make these macros better? Will any trivial modifications enhance our editing capabilities? While the essentials macros above will cover most cases when writing a manuscript, we have a whole pile of possibilities to make them better.
Quick Cut or Copy variations?
On either macro, we could instead Cut or Copy the Selection to the clipboard or write a separate macro for each version, if you like. I prefer to have all versions available as long as I have reasonable shortcuts, but I prefer the above "Delete" versions do not mess with the clipboard.
What about paragraphs?
Working through the macro, it's easy to change out the sentence manipulation unit constant for a paragraph. I don't need the paragraph version as often, but it has come in handy a few times. Given that it requires a tiny expenditure of effort, it's an excellent extension of the base macros.
Handle punctuation marks more intuitively
We did a similar delete full sentence macro, and we also created a separate member version to accommodate dialog or parentheses. When deleting only part of a sentence, I prefer to delete just the text of a sentence not the ending punctuation. The alternative macro above does this, but the more naive method is tricked by some decimal numbers or abbreviations with periods. Word has some underlying algorithms to determine sentence ranges, and we can leverage these to be more rigorous with detecting the proper end of sentence punctuation. Not that Word is perfect at it, but we should be able to catch most (or at least many more) cases.
We can further include parentheses for more work-related documents and quotes to naturally handle dialog.
Check for a selection before deleting?
Since we're deleting the selection, one little gotcha that can pop up occasionally is: what happens if we end up with an empty selection? If so, the Delete action will still delete something. More specifically, it will delete whatever character is next to the insertion point.
Undo records
We could encapsulate the two changes into an undo record, so the undo shortcut only needs to be used once to reverse the changes. This is more of a user-friendly tweak since there are only two changes to the selection, but it's a nice touch. However, please read the tutorial carefully since undo records can also cause significant problems if they are not correctly implemented.