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 May 12, 2015)

4

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

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

Password Protection Loophole

Make sure your current and backup files are protected. Follow these steps to ensure the protection.

Discover More

Entering the Current Time

Need to enter the current time into a cell? It's easy to do using this keyboard shortcut. The shortcut is a handy one to know ...

Discover More

Reapplying Styles Seamlessly

Styles are a very powerful feature in Word, as they allow you to consistently apply complex formatting throughout a document ...

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 (ribbon)

Moving to the Start or End of the Real Document

The main body of your text is only one part of what makes up the entire document. Documents can consist of other elements, as ...

Discover More

Understanding the Select Case Structure

Programming structures are an important tool used by any programmer. The VBA language used by Word's macros includes several ...

Discover More

Assigning a Macro to a Shortcut Key

Do you have a macro that you use frequently? Using the file menu to access the macro can be time consuming. This tip talks ...

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 8Mpixels. 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 7 - 0?

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.