• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 43. Building Custom Applications... > Displaying Messages in Word with VBA

Displaying Messages in Word with VBA

A more complex message box includes the message text, a title, and a description of the buttons that should appear. For example, let's assume you want to create a message that asks, “Do you want to print the current document?” with buttons for Yes and No. Follow this procedure to add that message box to a Word document:

Start Word and press Alt+F11 to bring up the VBA Editor. In the Project Explorer, navigate to a module in the Normal project, Normal.dot.

Choose Insert, Procedure, type OKToPrint, and press Enter. You'll have a new subroutine called OKToPrint(), with the Sub/End Sub pair provided by VBA.

Start typing the following program. Note how the ScreenTip appears (see Figure 43.11) to show you the components available in MsgBox().

Public Sub OKToPrint()
i = MsgBox("Want to print the current document?", vbYesNo)
If i = 6 Then ActiveDocument.PrintOut
End Sub

Figure 43.11. VBA's ScreenTip follows along as you type, giving you hints for the parameters valid in a MsgBox().

If you're having trouble building a message that uses buttons other than Yes and No, see the “Troubleshooting” section at the end of this chapter.

To run OKToPrint, click the Run Sub/User Form button (or press F5). Test it several times by clicking Yes and No, pressing Enter, tabbing between the Yes and No buttons, and even pressing the Esc key to abort the message box (see Figure 43.12).

Figure 43.12. MsgBox() returns a value of 6 if the user clicks Yes. To see a complete list of all valid MsgBox() return values, position the insertion point within MsgBox() and press F1.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint