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.
Discover the Power of Microsoft Office This beginner-friendly guide reveals the expert tips and strategies you need to skyrocket your productivity and use Office 365 like a pro. Mastering software like Word, Excel, and PowerPoint is essential to be more efficient and advance your career. Simple lessons guide you through every step, providing the knowledge you need to get started. Check out Microsoft Office 365 For Beginners today!
When processing a document in a macro, you may need to make some of your text bold. It's easy to do using the Bold ...
Discover MoreDo you have a macro that you use frequently? Using the File menu to access the macro can be time consuming. This tip ...
Discover MoreOne of the math functions you can use in your macros is the Int function. It provides a way for you to derive an integer ...
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