Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. 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: Using Message Boxes.

Using Message Boxes

Written by Allen Wyatt (last updated May 22, 2021)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


2

When you create macros in Word, you can easily incorporate the use of message boxes. These are typically used to convey information to the user and to get some rudimentary input. You include message boxes by using the MsgBox command. The following portion of a macro creates a very simple message box:

MsgBox "The macro is done"

You can also add symbols to your message boxes by including a symbol-type code as part of your MsgBox invocation. These symbols are used extensively in many Windows dialog boxes. The following four types of symbols can be used:

Type Enumeration Symbol
16 vbCritical White X in a red circle (and "ding")
32 vbQuestion Question mark in a circle
48 vbExclamation Exclamation point in a circle
64 vbInformation Information symbol (lowercase i in a circle)

You can use either the number in the Type column or the enumeration in the Enumeration column with the MsgBox statement. As an example, let's suppose you wanted to include the exclamation point symbol. This is typically included in dialog boxes as a notice of when something important has happened or is about to happen. To include this symbol in your message box, you would include either of the following code lines:

MsgBox "Can't run the macro on the text", 48
MsgBox "Can't run the macro on the text", vbExclamation

So far, the MsgBox command has been used as a statement, but you can also use it as a function. If you do so, you can use it to get simple input from the user. To make the MsgBox function more useful, Word allows you to display more clickable buttons in the dialog box besides the OK button. This is done by adjusting the type code, which was used for the symbols displayed in the message box. The following are the different button combinations you can display in your message box:

Type Enumeration Button Types
1 vbOKCancel OK, Cancel
2 vbAbortRetryIgnore Abort, Retry, Ignore
3 vbYesNoCancel Yes, No, Cancel
4 vbYesNo Yes, No
5 vbRetryCancel Retry, Cancel

To use the buttons, you simply add the value of the button type to the value you want used for the symbol. You can use either the values in the Type column or the enumerations in the Enumeration column; VBA doesn't care which is used. In the previous example, you used the code of 48 or enumeration of vbExclamation to display the exclamation point symbol. If you wanted to also include the Abort, Retry, Ignore buttons, you could simply use the following code lines:

J = MsgBox("Can't run the macro on the text", 48 + 2)
J = MsgBox("Can't run the macro on the text", vbExclamation + vbAbortRetryIgnore)

If you choose to use numeric values, you can actually add the values together. In other words, you could use "50" instead of "48 + 2". After the code line is executed, J will be equal to a value that indicates which button was clicked. In doing your testing to see what J is equal to, it is best to use enumerations, but you could use values. Here are the possible return values:

Value Enumeration Button Clicked
1 vbOK OK
2 vbCancel Cancel
3 vbAbort Abort
4 vbRetry Retry
5 vbIgnore Ignore
6 vbYes Yes
7 vbNo No

Should you use values or enumerations with MsgBox? It really boils down to personal preference, but there are two major advantages to using enumerations. First, when you are typing the VBA code, the editor automatically offers "hints" as to the available enumerations. Second, the enumerations are more descriptive in your code, meaning you can more easily see what their effect is within MsgBox. Third, the enumerations protect you against any possible changes that Microsoft may make to how MsgBox works in the future. This isn't to say that Microsoft will make changes; the values shown in this tip have been static for years. But if they do change the values, the enumerations will continue to work because Microsoft simply changes the meanings of the enumerations behind-the-scenes.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (8931) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Using Message Boxes.

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

Document Shows as 'In Use' by Another User

Word tries to constantly track who is using various documents, in order to prevent two users from clashing in their edits ...

Discover More

Including Section Numbers in an Index

When you use Word to create your index, you'll normally only include a page number in the index. If you want to create an ...

Discover More

Saving in Two Locations

When you save a workbook to disk, you may want to automatically save a duplicate workbook in a separate location. This ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Repeating In a Macro

Macros are often used to process information stored in documents. Usually the processing involves some sort of iterative ...

Discover More

Controlling Repagination in Macros

Want to turn off document repagination while your macro does its work? Here are two approaches you can use.

Discover More

Repaginating in a Macro

When creating a macro that extensively processes a document, you may need to periodically force Word to repaginate the ...

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}] (all 7 characters, in the sequence shown) 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 5 - 3?

2021-09-08 09:52:06

Andrew

Yes, John, your question answers itself - just use the concatenation operator (&) to string together all your info. One trick to note is to liberally include vbCr into the output to make it more readable, like this:

msgbox "User name is " & UserName & "," & vbCr & " trust centre flag is " & TCFlag & "."


2021-09-03 16:26:44

John Ladd

Is it possible to concatenate multiple elements into a message box?
For example, I want to display:
* Some text
* The current user name (what shows in Track Changes and comments)
* The current status of a setting from trust centre
(and others, but these are just examples).

These are things I know how to set using a macro, but I'd like to be able to display their current status, with some text.

So for the example above I might hope to see something like:
"User name is John, trust centre flag is True."

Thanks
John


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.

Videos
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.