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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Using a macro to format your document (or portions of your document) is not all that uncommon. If you want your macro to ...
Discover MoreOne of the most common ways of creating macros is to use Word's macro recorder. This tip shows how easy it is to use the ...
Discover MoreYou can easily store your macros in a template. If you create a document based on the template and then the document is ...
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