Emulating the Msg Box Function

VBA's MsgBox function is a bit unusual because, unlike most functions, it displays a dialog box. But, similar to other functions, it also returns a value: an integer that represents which button the user clicked.

This section describes a custom function that I created that emulates VBA's MsgBox function. On first thought, creating such a function might seem rather easy. Think again! The MsgBox function is extraordinarily versatile because of the arguments that it accepts. Consequently, creating a function to emulate MsgBox is no small feat.

Note The point of this exercise is not to create an alternative messaging function. Rather, it's to demonstrate how to develop a relatively complex function that also incorporates a UserForm. However, some people might like the idea of being able to customize their messages. If so, you'll find that this function is very easy to customize. For example, you can change the font, colors, button text, and so on.

I named my pseudo-MsgBox function MyMsgBox. The emulation is close, but not perfect. The MyMsgBox function has the following limitations:

■ It does not support the Helpfile argument (which adds a Help button that, when clicked, opens a Help file).

■ It does not support the Context argument (which specifies the context ID for the Help file).

■ It does not support the system modal option, which puts everything in Windows on hold until you respond to the dialog box.

■ It does not play a sound when it is called. The syntax for MyMsgBox is

MyMsgBox(prompt[, buttons] [, title])

This syntax is exactly the same as the MsgBox syntax except that it doesn't use the last two optional arguments (Helpfile and Context). MyMsgBox also uses the same predefined constants as MsgBox: vbOKOnly, vbQuestion, vbDefaultButton1, and so on.

Note If you're not familiar with the VBA MsgBox function, consult the Help system to become familiar with its arguments.

0 0

Post a comment