Syntax
InputBox$(Prompt$ [, Title$] [, Default$])
Remarks
Displays a dialog box requesting a single piece of information and returns the text entered in the dialog box when the user chooses the OK button. If the user chooses the Cancel button, an error occurs. You can use the On Error statement to trap the error.
Argument | Explanation |
Prompt$ | Text displayed in the dialog box indicating the kind of information requested. If Prompt$ is longer than 255 characters, an error occurs. |
Title$ | Text displayed in the title bar of the dialog box (if omitted, Word uses the title "Microsoft Word"). |
Default$ | Text that initially appears in the text box of the dialog box. This value is returned if the user types nothing before choosing OK. If Default$ is longer than 255 characters, an error occurs. |
In Windows, if the user presses enter to start a new line while the dialog box is displayed, InputBox$() returns text containing Chr$(11) + Chr$(10) (a newline character followed by a paragraph mark) where the line break was made. For a method of removing the newline character, see the third example for this entry. On the Macintosh, if the user presses RETURN to start a new line, Chr$(13) is returned.
To choose the OK button in an input box using keys, press ENTER (Macintosh), or press TAB to move to the OK button, and then press ENTER (Windows).
Examples
This example asks the user to type a word, which is assigned to the variable word$. You could use this variable in a macro that counts the occurrences of a given word in the active document (as in the example for EditFindFound()).
word$ = InputBox$("Count instances of:", "WordCounter")
The following example prompts the user to type a number from 1 to 10. InputBox$() returns what the user types as a string, which the Val() function converts to a number to assign to the numeric variable num. Note that if the user spells out "Ten" instead of typing "10," Val() returns 0.
num = Val(InputBox$("Type a number from 1 to 10:"))
This example asks the user to type multiple lines, which are assigned to the variable a$. The CleanString$() function removes any extra newline characters (returned in Windows) so that line breaks are preserved as entered by the user.
a$ = InputBox$("Type multiple lines:") a$ = CleanString$(a$)
See Also
Input, MsgBox, On Error, Val()