We've covered the Selection in general in another article. For many macros, we want to take different actions depending on whether there is a selection or not in the document when the macro is run. Sometimes we take an extra step, but other times we just need to do different steps in either case.
We focus on selection types that involve text in one way or another. They aren’t an exhaustive explanation of all selection types, but they cover some of the most probable ones for writers.
Thanks for your interest
This content is part of a paid plan.
Detecting the Selection Type
Specifically, we need to check the Selection’s Type property which has different values depending on the current selection in the active document. Microsoft has a list of Selection Type constants, but the descriptions are rather terse.
Thus the reason for this wonderfully and amazingly helpful article.
The most important case for writers is probably just to detect whether there is a normal, everyday text selection or not; so we focus on that case.
Normal selection
If Selection.Type has a value of wdSelectionNormal, it is a regular text selection, but be aware of the caveats below if you have more complicated documents.
That is, we can validate the selection type using an If condition
To take different steps in either case, whether a selection exists or not, include an Else inside the If statement.
Now for the caveats.
Other normal selection cases
For fiction writers, we don’t often include shapes or tables in our documents, but many non-fiction writers may; so check below if you plan to use either of them.
Unfortunately, a normal selection can be a little ambiguous in general.
Text inside shapes
Selected text may be inside a shape that allows text. As long as the shape is not selected specifically, it will still count as a normal selection (see full object selections below).
Text inside tables
The selected text may be inside a table as long as a full cell, row, or column is not included in the selection (see full object selections next).
Full object selections
Unfortunately from a macro perspective, it appears an entire shape can be included in a selection when it is only part of one that spans at least some regular document text. In this case, the entire selection will still count as a normal selection type.
Similarly, an entire table will be classified as a normal selection if it includes some document text outside the table.
Non-contiguous selections
Unfortunately, non-contiguous selections also show up as a normal selection type (see more below).
Not a Selection
If you only need to do something different if there isn’t a selection, then the wdSelectionIP Type constant corresponds to “no selection” in the intuitive sense. That is, no text or objects are selected in the document, and a blinking I-bar at the insertion point waits for your typed text.
Similar to a normal selection type, if you want to take (often extra) steps only when no starting selection exists, you can use
What about inside shapes and tables?
If the insertion point (with no text selected) is inside a shape or a table, the selection type will still be wdSelectionIP.
The distinction shows up when the shape itself, or whole columns or rows of a table are selected, but that wouldn’t be a text insertion point anyhow.
Avoid using wdNoSelection type
If you check the Selection Type constants, you might notice a skipped constant wdNoSelection.
What?
Why did he use wdSelectionIP? This dude’s a fraud …
Well, that may be up for debate, but it’s probably best to not use wdNoSelection since it does not seem to work in the intuitive sense of its name.
It seems to be a literal lack of a selection somehow, but the exact usage is not clear from a regular user’s perspective, and Microsoft’s descriptions of the constants are terse.
If you don’t believe me, snag the macro below and do a few tests. It is difficult to create a case in a regular working document that has the wdNoSelection type, so just use wdSelectionIP if you want to detect no selection in the practical sense.
It’s a little goofy, but at least wdSelectionIP works the way you expect when you want to check for no selection in a regular, everyday document.
Special Case Text Selection Types
We are focusing here on text-based selection types.
Non-contiguous selections
Recall you can add non-contiguous selections using the Command key on Mac or Control on Windows and clicking or dragging across more text even if the text is not connected to the original.
Checking non-contiguous selections
Unfortunately, a non-contiguous selection counts as a normal section type, so it doesn’t appear that we can (as yet) detect in a macro when the user has selected multiple discontinuous portions of text.
If you try to access the discontinuous selection’s text property, you will get the last addition’s text.
Guarding against non-contiguous selections
Word VBA doesn’t appear to give us much control in a macro over non-contiguous selections (as of the date of this article in January 2024), but if you need to cancel such a selection without just collapsing the selection entirely, the method
shrinks the selection to the most recent addition.
Fortunately, if there is an insertion point or only one selected segment of text, nothing happens when using ShrinkDiscontiguousSelection, so you could include the command just in case without fear of ruining the starting selection in regular use cases.
Non-contiguous selections are a nice feature in Word, but I generally haven’t worried about guarding against them when creating my own writing macros.
Block selection type
The Selection type wdSelectionBlock corresponds to special block selections.
Creating block selections
Recall if you hold the Option on a Mac (or Alt on Windows) and select using the mouse, you can drag a selection making a rectangular block spanning different lines of text.
Alternatively, you can press Command+Shift+F8 on a Mac or (Control+Shift+F8 on Windows) and use the arrow keys to select the block of text.
About block selections
Block selections are very nice when you need them, but the use cases don’t pop up often for writers in my experience, and they are probably even rarer to try to handle in a macro.
They don’t appear to be treated the same as regular non-contiguous selections in a macro, but they work similarly in practice. Word will act on all pieces of selected text depending on what command you give.
Accessing the Selection's text property in a macro when it is a block selection will make it act weird (the selection expands in a strange way to include all covered paragraphs in addition to the original block selection). The text property also only contains the first partial line of the selection text anyhow, so it’s probably best not to use block selections in macros.
Guarding against block selections
Word VBA doesn’t appear to give us much control over block selections in macros (as of the date of this article in January 2024), so if you want to avoid any issues if the user happens to have a starting block selection, you can just collapse it if one is detected.
The Selection.ShrinkDiscontiguousSelection does not appear to work with block selections, so Word doesn’t seem to think of them as regular non-contiguous selections.
I haven’t guarded my own macros against potential block selections, but you might have a special case where it could cause problems.
Validating Selection Types
The following is a simple, quick macro that checks what type of selection your particular situation has. Nothing groundbreaking, but it will save you some time if you want to do a quick test or validation when creating a macro.
As far as I know, a selection can only have one type.
If this were real VBA code for one of my writing macros, I would use a Select Case statement instead of a stack of If statements, but I did not want to introduce a new command just for a quick and dirty testing macro.