Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Getting User Input in a Dialog Box.

Getting User Input in a Dialog Box

by Allen Wyatt
(last updated February 16, 2017)

2

If you need to get input from a user under control of a macro, one method you can use is to employ the InputBox function. This function displays a dialog box and allows the user to type a response. The result is a string, returned to your macro, which you can then process and use.

The syntax for the InputBox function is as follows:

sMyString = InputBox(sPrompt, sTitle, sDefault)

There are three parameters you can use with InputBox, although only the first one is absolutely required. In this syntax, sPrompt is the text you want displayed as the user prompt, sTitle is the text to display in the title bar of the dialog box, and sDefault is the default text string offered to the user in the dialog box. The user can edit or accept the default string, as desired.

As an example, the following code lines can be used to display a dialog box and ask the user for his or her name:

sPrompt = "Please check your name and make any corrections"
sTitle = "Name Entry"
sDefault = "John Doe"
sUserName = InputBox(sPrompt, sTitle, sDefault)

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1356) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Getting User Input in a Dialog Box.

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

Moving the Insertion Point to the Beginning of a Line

If you need to move the insertion point within your macro, then you'll want to note the HomeKey method, described in this ...

Discover More

Using Track Changes

Track Changes is a valuable Word tool that allows you to automatically mark changes in your document. This is a great boon ...

Discover More

Creating Long Page Footers

Ever wish that you could create nice, long footers that appear at the bottom of each page when you print your worksheet? ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (menu)

Highlighting Every Thousandth Character

Not satisfied with the detail provided by the Word Count feature in Word? Perhaps you want to actually know where every one ...

Discover More

Extracting INCLUDEPICTURE File Names

If you use the INCLUDEPICTURE field to add images to your document, you may love the macro in this tip. It allows you to pull ...

Discover More

Offering Options in a Macro

When creating macros, you often need to offer a series of choices to a user. This tip demonstrates how easy it is to offer ...

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

2017-02-20 09:24:12

Alan Elston

Just a passing note: I like to use Named arguments to help me remember in these cases what the arguments are
For the above example this would be, for the first three
(Prompt:= sPrompt, Title:= sTitle, Default:= sDefault)
( The full list is given here: msdn.microsoft.com/en-us/library/office/gg251811.aspx )

A characteristic of this is that for named arguments they may be in any order, such that, for example with the above example , if the Title was to be omitted, then
_ for named arguments we could use this syntax:
(Prompt:= sPrompt, Default:= sDefault) or (Default:= sDefault, Prompt:= sPrompt)
but
_ for unnamed arguments the order must be maintained, with place holders included as necessary for any missing arguments in between.
So for this example it would be
(sPrompt, , sDefault)

( Missing arguments must be omitted at the end - This , ) would cause an error as , , is needed, I think, to be recognised as a place holder.
With no following , the arguments are taken as complete, ( assuming any missing are optional ) )


2017-01-26 10:06:48

fj1

Is there a function to input single character?
I need this because after input one character and the macro continue to run.
As that (Char function) used in WordPerfect.


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon 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.