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: Determining if a Text Selection Exists.

Determining if a Text Selection Exists

by Allen Wyatt
(last updated February 26, 2018)

5

There are numerous times when you are writing macros when you will need to determine if the user has made a selection. The following VBA code will let you know if a selection has been made. The variable designated to be a flag (InSelection) is first set to False, and then the Type property of the Selection object is used to determine if there is a selection. If so, then InSelection is set to True.

InSelection = False
If Selection.Type = wdSelectionIP Then InSelection = True

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 (8927) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Determining if a Text Selection Exists.

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

Merging Many Workbooks

If you need to combine the contents of a bunch of workbooks into a single workbook, the process can get tedious. Here's a ...

Discover More

Out of Kilter Borders

Borders not printing properly? It could be any one of a number of reasons causing the problem. This tip provides some ...

Discover More

Changing Chart Size

Place a chart on a worksheet and you may not be satisfied with its size. Changing the size of a chart is a simple process ...

Discover More

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!

More WordTips (ribbon)

Writing a Macro from Scratch

Creating macros can help extend what you can do in Word. If you work with macros, you know that creating macros from ...

Discover More

Repaginating in a Macro

When creating a macro that extensively processes a document, you may need to periodically force Word to repaginate the ...

Discover More

Determining the Day of the Month

When writing macros, you may need to know which day of the month a particular date represents. Here's how to use the Day ...

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 5 + 9?

2017-10-20 11:37:35

Big Fan

Thank you Rick and George for posting working code!


2015-05-12 14:29:59

Derek Brown

For what it's worth, I have for many years nested as required a macro "SIP" which sets a public variable "IP" at 1 if the cursor is at an Insertion Point, and at 0 if not -- macro name and variable name are easier to remember than the lengthy vba codes.

Same sort of macro for whether the cursor is in a table ("SIT", "IT") or at the top of a document. Similarly, my "SSEL" macro generates variables for what the features of a Selection are (length "LSEL", leftmost character and its ASCI, rightmost character and its ASCI, Trimmed length etc.).


2015-05-12 11:27:30

Rick G.

I agree with George, Allen wrote the logic the opposite of what it should be.

Also, you could skip the initialization statement:
InSelection = False
and just write:
InSelection = Selection.Type <> wdSelectionIP


2015-05-12 10:01:38

George

This is exactly the opposite of the correct test; wdSelectionIP indicates the selection is an insertion point, i.e., not in a selection. The correct code should be
If Selection.Type <> wdSelectionIP Then InSelection = True


2015-04-16 19:00:44

David

How to determine if the selection is a paragraph?


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.