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: Getting User Input in a Dialog Box.

Getting User Input in a Dialog Box

by Allen Wyatt
(last updated December 3, 2014)

4

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 (6202) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word 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

Copying Character Formatting

If you are applying character formatting directly to text rather than using a character style you can copy it from one place ...

Discover More

Getting Rid of Stubborn Icons

Ever tried to clean-up the icons on your toolbar only to have one that won't go away? This tip explores some possible ...

Discover More

Resizing Your Table

Need to make your table a different size? It's easy to do, using the same general technique you use when resizing a graphic. ...

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)

Determining an ANSI Value

You may need to determine the numeric value of a character in a macro. You can do that using the Asc function, described in ...

Discover More

Accessing Paragraphs in a Macro

Need to process a document, paragraph by paragraph, in a macro? It's easy to do once you understand that Word's object model ...

Discover More

Removing a Macro from a Shortcut Key

Associate a macro with a shortcut key, and at some time you may want to break that association. (Perhaps so the shortcut key ...

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

2017-02-20 09:28:58

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


2016-01-25 18:46:13

Kristie

The input box works well, but does not return the input string to the document.


2015-11-29 13:20:56

Ruslan

Hi!
I have a small sub here (take a line and shows it in every available font), which i took somewhere from MS site, it works fine, exept i would like it to ask line i need to show, so i used method u describe here, and when i try to change .TypeText "Suum Cuique" to .TypeText sTexttoDisplay
it start to show much less fonts then w just a text. What possibly can be the issue? Thx in advance :)

Sub ListFonts()

Dim varFont As Variant
' Speeds macro processing and suppresses display.
'Application.ScreenUpdating = False
' Create new document.
Documents.Add Template:="normal"
'Asking for text to show in different fonts
sPrompt = "Enter text to display in different fonts"
sTitle = "Enter Text"
sDefault = "default text"
sTexttoDisplay = InputBox(sPrompt, sTitle, sDefault)

' Loop through each available font.
For Each varFont In FontNames
With Selection
' Format for name of font.
'.Font.Name = "times new roman"
'.Font.Bold = True
'.Font.Underline = True
' Insert Font name.
'.TypeText varFont
' Insert a new paragraph after the Font Name.
'.InsertParagraphAfter
' Move to the new paragraph.
.MoveDown unit:=wdParagraph, Count:=1, Extend:=wdMove
' Format for the font example.
.Font.Bold = False
.Font.Underline = False
.Font.Name = varFont
.Font.Size = 48
' Enter example text(Alphabetic characters.)
.TypeText "Suum Cuique" 'sTexttoDisplay
' Insert a new paragraph.
.InsertParagraphAfter
' Move to the new paragraph.
'.MoveDown unit:=wdParagraph, Count:=1, Extend:=wdMove
' Insert example text(Numeric characters.)
'.TypeText "0123456789?$%&()[]*_-=+/<>"
' Insert two new paragraphs and move down.
'.InsertParagraphAfter
'.InsertParagraphAfter
.MoveDown unit:=wdParagraph, Count:=1, Extend:=wdMove
End With

Next varFont
Application.ScreenUpdating = True

End Sub


2015-03-11 17:44:31

Frank Tucker

What I'm looking for is a method of using content controls and storing the text string value of the control on exit to a bookmark to be referenced within the document (headers, footers, etc.)

Is this possible?


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.