Displaying Messages and Requesting Information

WordBasic includes several statements and functions you can use to communicate with the user. This section explains the Print, MsgBox, MsgBox(), InputBox$(), and Input statements and functions, each of which provides a different way to display a message or to request information. Here's a brief overview of the capabilities of each one:

You can also create your own dialog boxes with check boxes, list boxes, and other features. For more information, see Chapter 5, "Working with Custom Dialog Boxes."

Note

Print and Input can also be used to insert information into or read information from text files. For information on using Print and Input in this way, see "Using Sequential File Access" in Chapter 9, "More WordBasic Techniques."

Print

The Print statement displays a message in the status bar. Its most common use is to display a message describing the status of the macro. For example, if a macro needs time to perform a task, you might use the Print statement to display the message "Working…" in the status bar so the user understands that something is indeed happening.

The instruction Print "Working..." displays the following message in the status bar.

The Print statement accepts strings, string variables, numbers, and numeric variables, and allows you to mix them together. Here are some examples of valid Print instructions:


Print "Hello"
Print Name$
Print 365.25
Print Total

The Print statement can also display multiple items separated by commas or semicolons. When you use commas as delimiters, Word inserts a predefined tab space between the items. When you use semicolons as delimiters, the next item starts immediately after the previous one. For example:


Print "Hello, "; Name$, "The total is "; Total

MsgBox and MsgBox()

The MsgBox statement displays a message in a message box that the user must acknowledge before the macro can continue. You can provide a title for the message box and specify a symbol that identifies the type of message you want to display. For example:


MsgBox "The macro is finished.", "Sample Macro", 64

This instruction displays the following message box.

In the previous MsgBox instruction, "Sample Macro" is the message box title and 64 is a number that controls which symbol and buttons are displayed. For more information on using these arguments, see MsgBox in Part 2, "WordBasic Reference."

The MsgBox() function is just like the MsgBox statement, except that you can use it to give the user a choice or to ask the user a question. For example:


answer = MsgBox("OK to reformat?", "Two Column Macro", 292)

This instruction displays the following message box.

If the user chooses the Yes button, MsgBox() returns one value; if the user chooses the No button, MsgBox() returns a different value. The values are placed in the variable answer. The numeric argument 292 specifies the symbol and buttons displayed.

InputBox$()

InputBox$() displays an input box in which the user can type a response to a prompt. You can provide a title for the input box, a prompt, and a default response. For example:


DocName$ = InputBox$("Name of the file to open?", "Open Macro", \
        "Latest memo")

This instruction displays the following input box.

The user can replace the default response or accept it. The response is placed in the variable DocName$. Note that the user can type more than one line in an input box and can type enter to begin a new line. If the user presses enter to begin a new line, either Chr$(11) + Chr$(10) (Windows) or just Chr$(11) (Macintosh) is included in the string InputBox$() returns. In Windows, the Chr$(11) + Chr$(10) combination is interpreted as two paragraph marks when inserted into a document; in effect, an extra paragraph mark is inserted. You can use the CleanString$() function to remove the extra paragraph mark, as shown in the following example:


Address$ = InputBox$("Please type an address:", "Letter Macro")
Insert CleanString$(Address$)

Input

The Input statement uses the status bar to prompt the user for information. Because it's easy to overlook a prompt in the status bar, InputBox$() is generally preferred over Input. However, Input is a little more flexible than InputBox$(). Input can assign the user's response to numeric or string variables, whereas InputBox$() only returns strings. For example:


Input "What point size for the headline", Size

This instruction displays the following prompt. Notice that Input adds a question mark to the prompt.

The user's response is placed in the numeric variable Size.

You can prompt for a list of variables by separating each one with a comma. For example:


Input ItemNumber, Quantity
Input UserName$, ID

The user must respond with appropriate values separated by commas. The following are examples of acceptable responses:


1234,3
Mark Kyle, 6823

Input always attempts to divide the user's response at a comma. If you want to allow responses that contain commas, use a Line Input instruction. Line Input works like Input, but with one important difference: You can specify only a single string variable. For example:


Line Input Address$

If Word encounters a comma in the response, it returns the comma as part of the string that is assigned to Address$.