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)

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

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

Preventing Changes to Styles in Documents

Have you ever created a template only to have the styles within it changed as they were used within a document? Here are some ...

Discover More

Determining Differences Between Dates

Macros are often used to process the data in a worksheet. If that data includes dates that need to be processed, you'll ...

Discover More

Moving Objects in Word 2007 without Snapping to the Grid

When you use the mouse to move objects around, they normally "snap" to the invisible grid that overlays your document. If you ...

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)

Using Call to Run VBA Macros

An elegant way to run macros from within macros is to use the Call statement. In order to use it, you need to provide a ...

Discover More

Understanding Variables

Variables are used quite frequently in programming, and VBA programming is no exception. Each variable can have a data type ...

Discover More

Creating a New Document in VBA

When working with documents in a macro, it makes sense that you may need to create a document from time to time. Here's how ...

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 6 + 0?

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.