Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, and 2016. 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.

Checking for a Text Selection Length

by Allen Wyatt
(last updated June 9, 2018)

5

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 Selection.Type=wdSelectionIP Then
Inselection=True
End If

Or, If you prefer:

Inselection=(Selection.Type=wdSelectionIP)

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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, and 2016. You can find a version of this tip for the older menu interface of Word 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

Resizing Checkboxes

If you create a user form in VBA that includes checkboxes, you may want to make the checkboxes larger. You can't adjust ...

Discover More

Excel Conditional Formatting

Conditional formatting allows you to modify how information is displayed based upon criteria that you define. Excel ...

Discover More

Develop Macros in Their Own Workbook

If you develop macros and edit them quite a bit, you may be running the risk of causing problems with the macros or with ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Swapping Two Strings

Part of developing macros is learning how to use and manipulate variables. This tip examines a technique you can use to ...

Discover More

Can't Edit Macros

Load up documents created on older versions of Word, and you may find that you can't edit the macros you are used to ...

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

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

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. Maximum image size is 6Mpixels. 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 9 + 2?

2016-04-18 21:21:23

Av

What about
if selection.start <>selection.end
?


2014-05-07 13:12:34

awyatt

Seems that it is best to NOT check the length. Instead, use the following code:

Inselection = False
If Selection.Type = wdSelectionIP Then
Inselection = True
End If

Or, if you prefer:

Inselection = (Selection.Type = wdSelectionIP)

-Allen


2014-05-07 12:50:05

Marc Hichens

I can confirm that the code also does not work in Word 2010. When nothing is selected (flashing cursor), the selected range has a length of 1. When 1 character is highlighted, the selected range also has a length of 1.

I am looking for a technique to check whether any text is highlighted prior to proceeding with the remainder of the code.


2012-12-16 15:01:49

Moshe Flam

In any case this does not work for word 2003 (and I suspect doesnt work for all versions) - When nothing selected, the first letter after the cursor is returned, so you get Len(Selection) = 1 and not zero.


2012-10-06 05:22:37

Rod Grealish

The code fragment

InSelection = False
If Len(Selection) > 0 Then InSelection = True

could be written as

InSelection = Len(Selection) > 0


This Site

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.

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.