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.
Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2021 or Microsoft 365. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word Step by Step today!
Sometimes it is helpful to know how often a particular phrase appears within a document. If you need to know such a ...
Discover MoreWord allows you to create a macro that is run automatically whenever the program is started. If you want to bypass the ...
Discover MoreWhen writing a macro to process the text in a document, you may need to move the insertion point to the end of a line. ...
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