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.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
There are a myriad of ways in which a date can be formatted: day first, month first, number of digits in the year, etc. ...
Discover MoreWhen you record a macro, Word very literally records what you do. This includes filling in various settings in dialog ...
Discover MoreDeclaring variables in a macro is good programming practice. Here’s how to do it and how to make sure they are all ...
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