Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Checking for a Text Selection Length.

Checking for a Text Selection Length

by Allen Wyatt
(last updated October 6, 2012)

2

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 (740) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Checking for a Text Selection Length.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Clearing Large Clipboard Entries

Need to clear out a large amount of information saved on the Clipboard? All you need to do is to replace it with a small ...

Discover More

Nudging a Graphic

Want to get a graphic to just the right position on a worksheet? Sometimes the easiest way is to use the arrow keys on the ...

Discover More

Converting Phone Numbers

Sometimes you receive a phone number that contains alphabetic characters and you need to convert it to a purely numeric phone ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

MORE WORDTIPS (MENU)

Counting Characters in a Selection with VBA

Need to figure out the number of characters in a range of selected text? Here's how to do it in VBA.

Discover More

Determining the Current Page Number

While your macro is processing the text in your document, you may need a way to determine the current page number where the ...

Discover More

Determining the Number of Fonts Available

When creating a macro, you may need to figure out how many fonts are available to Word. You can do this using the FontNames ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments for this tip:

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is eight more than 7?

2013-04-10 11:47:28

Steve Lang

Use len(selection.range)instead.


2012-12-16 14:57:44

Moshe Flam

Doesn't work. When nothing selected returns (at least in msword 2003) the first letter after the the cursor.


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.

Links and Sharing
Share