MsgBox, MsgBox()

Syntax

MsgBox Message$ [Title$] [Type]

MsgBox(Message$ [Title$] [Type])

Remarks

The MsgBox statement displays a message in a message box. You can also display a message with the MsgBox() function, which returns a value according to the command button the user chooses in the message box. Use MsgBox() if you need your macro to take action based on the user's response.

Argument

Explanation

Message$

The message to be displayed in the message box. If Message$ is longer than 255 characters, an error occurs.

Title$

The title of the message box. In Windows, if Title$ is omitted or is an empty string (""), "Microsoft Word" is the default title. On the Macintosh, if Title$ is an empty string (""), a title bar does not appear in the message box.

Type

A value representing the symbol and buttons displayed in the box.


Type is the sum of three values, one from each of the following groups.

Group

Value

Meaning

Button

0 (zero)

OK button (default)

1

OK and Cancel buttons

2

Abort, Retry, and Ignore buttons

3

Yes, No, and Cancel buttons

4

Yes and No buttons

5

Retry and Cancel buttons

Symbol

0 (zero)

No symbol (default)

16

Stop symbol

32

Question symbol (Windows) or attention symbol (Macintosh)

48

Attention symbol

64

Information symbol

Button action

0 (zero)

First button is the default

256

Second button is the default

512

Third button is the default


By specifying Type as –1, –2, or –8, you can display the message in the status bar instead of a message box. This is similar to using the Print statement, but gives you more control over how long the message is displayed. Specifying Type as –1 displays the message until another message replaces it; specifying –2 displays the message until a mouse or keyboard action occurs; and specifying –8 displays the message in the entire status bar width until a mouse or keyboard action occurs.

Because the MsgBox statement does not return a value, the use of button values other than 0 (zero) is not recommended. To make use of buttons other than the OK button, use the MsgBox() function. MsgBox() returns the following values.

Return value

Button chosen

Button text

–1

First (leftmost) button

OK

Yes

Abort

0 (zero)

Second button

Cancel

No

Retry

1

Third button

Cancel

Ignore


If Type is a negative value—that is, if you use MsgBox() to display a message in the status bar—MsgBox() always returns 0 (zero).

Examples

In this example, the MsgBox instruction displays a message box containing the message "Unable to find file," the title "MyTest Macro," an OK button, and a Stop symbol (0 + 16 + 0 = 16):


MsgBox "Unable to find file.", "MyTest Macro", 16

In the following macro, the If conditional checks whether or not there is a selection before proceeding. If there is no selected text, the MsgBox() function displays a message box asking if the user wants to continue anyway. The second If conditional tests the return value. If it's 0 (zero), which means the second button (No) was chosen, the macro ends without running subsequent instructions.


Sub MAIN
'Series of instructions that select text
If SelType() <> 2 Then
button = MsgBox("There is no selection. Continue anyway?", 36)
If button = 0 Then Goto bye
End If
'Series of instructions that act on the selection
bye:
End Sub

See Also

InputBox$(), Print