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

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

Status Bar Icons

Near the center of the status bar Word displays a number of different icons. This tip describes the meaning of each ...

Discover More

Getting the Proper Type of Ellipses

Type three periods in a row, and the AutoCorrect feature in Word kicks in to exchange that sequence for a special ...

Discover More

Creating Compound Characters

Word provides access to a wide variety of characters either from the keyboard or from the Symbol dialog box. Up and above ...

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)

Understanding the For ... Next Structure

Spend any time creating Word macros, and sooner or later you will need to repeat some of your programming code a certain ...

Discover More

Copying a Range of Pages in a Macro

Do you need to copy, within a macro, a range of pages? Because pages can be so fluid in Word, this can be a bit tricky. ...

Discover More

Removing Shortcuts for Macros

Word apparently has a bug in it that makes it very difficult to remove a shortcut key previously associated with a macro. ...

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 eight minus 1?

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.