Some of my favorite custom Word macros work with sentences akin to how the standard keyboard shortcuts manipulate words or paragraphs. Some hidden standard Word commands exist to handle sentences, but we can take them a step further with our own macros.
Thanks for your interest
This content is part of a paid plan.
Delete the current sentence
Sentences are rather fundamental to writing. Yeah, master of the obvious, so why doesn't Word have more stock tools for manipulating them like words or paragraphs? So, the job falls to us, but a nice side effect is we get to make Word work our way and control all the details.
Word has some standard (almost hidden) commands for selecting or navigating between sentences. These are covered in another article, but how can we accomplish our specific task better with a Word macro?
How deleting a sentence is usually done …
How do we usually delete sentences in a Word document? We grab the mouse and get to work.
It's feels fast enough when you're doing it, but wouldn't just tapping Alt+Shift+Delete (my assigned shortcut) be a lot faster? It's a repetitive task with specific boundaries, so why not let Word handle the details?
Delete the current sentence with a shortcut
How do we delete the current sentence using a macro?
Your hand never needs to move to the mouse or the arrow keys. Let's get started.
Create the empty macro
A previous article covered creating an empty Visual Basic for Applications (VBA) macro. When you’re done, you’ll have something like:
The single quote on the second line tells VBA the rest of the text on that line is a comment meant for human readers. The empty line is just patiently waiting for our amazingly efficient commands.
Getting started
We'll assume the user has an insertion point in the sentence to be deleted. Technically, a small selection within the sentence will also work, but we'll assume no initial selection for simplicity.
Select the sentence
The Selection (see a summary article for more details) is VBAs representation of the selection we see on screen when using Word. The VBA Selection includes additional data (properties) and actions (methods) to track and manipulate the user selection inside Word.
Specifically, we want to expand the selection to include the entire current sentence, so we can delete it. In VBA, the command is almost exactly what you might guess:
Unfortunately, the default unit for the Expand command is to expand the selection around the current word. We want the current sentence, so we need to tell the command the desired unit.
Remember the dot "." references specific data (properties) or actions (methods) of the thing (called an object) on the left. In this case, Expand is an action we can use with the document selection.
This is where Word macros can feel a tad more like programming, but it's not too bad. The resemblance is there, but don’t worry. I’m guiding you the whole way, and you’ll be editing faster when you're done.
Specify the Unit
How do we tell the command the correct unit to use? We literally give it a sentence Unit like so:
The main Word unit constants we’ll use in upcoming macros are: wdCharacter, wdWord, wdSentence, and wdParagraph. The unit constants all start with a “wd” prefix for “Word” to identify them as Word VBA constants.
We used a colon := equals symbol to specify the Unit since it is technically assigning the sentence constant’s value to the Unit parameter. If that doesn’t make much sense yet, just put the := when you are setting a value for a parameter in a command like this.
Expand caveat
The Expand command will not extend beyond the given unit. That is, it only expands once. If the selection already spans from the beginning to the end of the given unit, the selection will not change. This behavior is both useful and slightly annoying in some cases, but overall, it's just a feature we have to work around.
This differs from the keyboard behavior of the F8 expand shortcut which will incrementally expand the selection to larger document units (although F8 only increments in terms of standard Word content units).
Delete the selected sentence
Once we have the sentence selected, we just need to delete it using the Delete method of the Selection.
Gotchas?
We should always consider special cases just in case something could go wrong.
What if an initial selection exists?
If the initial selection is not empty but is still contained within a single sentence, perhaps around a single word, the Expand command will still expand over the current sentence. This behavior is exactly what we want, so no problem here.
What if the initial selection spans more than one sentence?
If the initial selection spans a sentence break, then the Expand command will extend the selection both ways to the beginning of the first sentence and to the end of the last sentence in the selection. This behavior works well for this macro since we do not have to do anything special to handle such selections. The macro will intuitively delete all partially selected sentences. Not only is this not a problem, it's a nice bonus feature that requires no extra work on our part!
Finish the macro
Now put the two commands together inside the empty macro up top, so the final macro is
I assigned this macro to Option+Shift+Delete on a Mac (or Alt+Shift+Backspace on Windows).
Even better, the finished version leverages the default behavior of the Expand command to work easily with multiple sentences. The user can even be sloppy with their selection, and the macro will intuitively detect the sentence boundaries and delete them.
And we're done.
I think you’ll like it. I sure do. I use it often, and I can’t imagine why Word does not include them by default. Now, go forth and edit faster!
Improvements
Many macros can be naturally extended to make them more useful, and sometimes the extension only requires a little extra effort.
Intuitively handle quotes or parentheses
The above macro is nice, but it deletes the entire sentence regardless of the structure of the surrounding paragraph. We could change the macro to detect and include or exclude double quotes or parentheses more naturally.
What does that mean?
When working in a novel, if a sentence only has double quotes on one side, then it's only part of the character dialog for that paragraph. It would feel more intuitive to delete the sentence but not the double quotes on the one side. Stated differently, only delete the double quotes if the entire sentence is dialog. A similar argument applies to parentheses in other document types.
This may sound strange when you first read it, but it provides a more intuitive behavior when editing a novel. It's also handy with parentheses in work-related documents. While not difficult, these changes are a little more involved, so they are relegated to a separate member article.
Cut and Copy variations
Of course, you could Cut or Copy the selection if you prefer. I’ve included all three in my macros just to have the flexibility.
I assigned my version of this to Option+Shift+X on a Mac (or Alt+Shift+X on Windows).
Copying the current sentence uses:
I assigned my version of this macro to Option+Shift+C on a Mac (or Alt+Shift+C on Windows).