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

Creating a Table of Contents from TOC Fields

If you inserted a bunch of TOC fields in your document, you can create your table of contents quite easily based on those ...

Discover More

Shrinking Cell Contents

Need to cram a bunch of text all on a single line in a cell? You can do it with one of the lesser-known settings in Excel.

Discover More

Creating a Lorem Ipsum Tool

When editing, you may need random placeholder text inserted in your document. Word provides a couple of ways you can do ...

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

Changing the Default Drive

Macros can be used to read and write all sorts of files. If those files are on a different drive than the current one, ...

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
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 three more than 5?

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.