Mastering the Message Box
(Page 1 of 6 )
It’s not uncommon for programmers to build scripts that communicate with the end user. Whether the script is delivering a notification or asking the user to make a choice, most programmers will turn to the simple message box. But there’s more to the message box than meets the eye! From controlling the title bar text and icon to its appearance and behavior, the message box provides many different customizable options. Today we’re going to explore all of those options—even the ones you won’t find in the MSDN documentation!
Message boxes are displayed by using the MessageBox function of the Win32 API. Both VBScript and Visual Basic for Applications provide a MsgBox function that allows limited use of the API MessageBox function.
The WshShell object’s Popup method, on the other hand, provides more thorough access to the MessageBox function. The MSDN documentation for the Popup method and the MessageBox function are incomplete at best, so learning to use them takes a little trial and error. Luckily, I’ve already done that for you. Let’s take a look at the syntax for the Popup method.
intButton = object.Popup(strText,[nSecondsToWait],[strTitle],[nType])
The Popup method has one required parameter, which is a string value containing the prompt to be displayed in the message box. According to the documentation, it returns an integer value that indicates the button that the user pressed. This is incorrect. It actually returns a Variant value that can be interpreted as a Long type integer, as I’ll explain later.
The Popup method also accepts three additional optional parameters.
nSecondsToWait is an integer value indicating the number in seconds the message box should display before timing out. Omitting this value or specifying 0 will cause the message box to remain on the user’s screen until they press a button.
strTitle accepts a string value that will be displayed in the title bar of the message box. If this value is Null (or omitted) the title will default to “Windows Script Host” in WSH or the API’s default “Error” in VBA.
nType accepts an integer value that represents a combination of bit flags specifying the various options for the message box. This is the most complicated part of the process, and we’ll talk about this in detail later in this article.
You can display a simple message box using the following code for WSH or VBA.
' WSH
Set WshShell = CreateObject("WScript.Shell")
intReturn = WshShell.Popup("Hello, world!")
' VBA
' Add a Reference to Windows Script Host Object Model
Dim WshShell As IWshRuntimeLibrary.WshShell
Set WshShell = New IWshRuntimeLibrary.WshShell
Dim lngResult As Long
lngResult = WshShell.Popup("Hello, world!")
Next: Controlling the appearance >>
More Visual Basic.NET Articles
More By Nilpo