Written by Allen Wyatt (last updated March 29, 2025)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365
There are numerous times when you are writing macros when you will need to determine if the user has made a selection. There are numerous ways you can do this in a macro. The following code illustrates one possible method for accomplishing this in VBA. It sets the value of a flag, bInSelection, to True if there is currently a selection. Otherwise bInSelection is set to False.
Dim bInSelection As Boolean bInSelection = False If Selection.Type<>wdNoSelection Then bInselection=True End If
You might wonder if you could use an approach of checking if the length of the selection is greater than zero, as in this method:
Dim bInSelection As Boolean bInSelection = False If Len(Selection) > 0 Then bInselection=True End If
This approach won't work. The reason is because when there is no selection made (the insertion point is collapsed and just blinking), Word still returns a selection length of 1. This is a bit aggravating as it makes no logical sense. Thus, you need to check the Type property of the Selection object, as in the first example above. If you want to shorten it even further, you could use the following:
Dim bInSelection As Boolean bInSelection=(Selection.Type<>wdNoSelection)
The approach used in this tip sets bInSelection to True if any selection of anything is made at all. You can further refine the selection status by understanding that the .Type property could be set to any of the following:
Enumeration | Value | Meaning | ||
---|---|---|---|---|
wdNoSelection | 0 | Nothing is selected. | ||
wdSelectionIP | 1 | An inline paragraph selection. | ||
wdSelectionNormal | 2 | A normal or user-defined selection. | ||
wdSelectionFrame | 3 | A frame is selected. | ||
wdSelectionColumn | 4 | A column is selected. | ||
wdSelectionRow | 5 | A row is selected. | ||
wdSelectionBlock | 6 | A block of text is selected. | ||
wdSelectionInlineShape | 7 | An inline shape is selected. | ||
wdSelectionShape | 8 | A shape is selected. |
These options mean that you can easily adjust your code to check for the presence or absence of a particular type of selection.
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12175) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Checking for a Text Selection Length.
The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2019. Spend more time working and less time trying to figure it all out! Check out Word 2019 For Dummies today!
Need to repeat an action a whole bunch of times? You can do it a time or two using keyboard shortcuts, but you'll need a ...
Discover MoreWhile your macro is processing the text in your document, you may need a way to determine the current page number where ...
Discover MoreMacros can make life easier, as they provide a fast and efficient way of processing text in a document. Such is the case ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments