bottom
Great WordTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > WordTips Home > Macros > VBA Examples > Checking for a Text Selection Length

Checking for a Text Selection Length

Summary: Shows how to figure out if the user made a selection before running the macro. (This tip works with Microsoft Word 97, Word 2000, Word 2002, and Word 2003.)

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 on 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.

Tip #740 applies to Microsoft Word versions: 97 | 2000 | 2002 | 2003


Step Up and Take Control! Subscribers to WordTips know just how valuable a resource it is. WordTips Premium provides twice the number of exceptional, easy-to-understand tips every week in an ad-free newsletter, as well as substantial discounts on WordTips archives and e-books.
 
Check out WordTips Premium today!

Helpful Links

Ask a Word Question
Make a Comment

Tips.Net Home
Vital News Home

WordTips FAQ
WordTips Premium

Learn Access Now

Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips

Advertise on the
WordTips Site

 

Great Info!

Get tips like this every week in WordTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)