Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Checking for a Text Selection Length.
by Allen Wyatt
(last updated April 18, 2016)
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, InSelection, to True if there is currently a selection. Otherwise InSelection is set to False.
InSelection = False If Len(Selection) > 0 Then InSelection = True
The technique used here relies on the fact that if a selection has been made, it will have a length. This length is returned by the Len function of VBA.
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 and 2010. 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 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!
Need to create a directory from within a macro? You can do it using a single command line, as detailed in this tip.Discover More
When processing a document with a macro, you may need to have the macro repaginate the text. It's easy to do using the ...Discover More
Program a macro, and you can easily find that some lines get very long. If you want to shorten the lines so they are more ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.