How Can I Display More Than 1,023 Characters in a Custom Message Box?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I need to create a custom alert box that has over 1,000 characters in it. The message box used by Wscript.Echo can hold all the characters, but I can’t change the message box title. I can change the title of a message box used by the VBscript MsgBox function, but it won’t hold all my characters. Can you help?

— KB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KB. Talk about a no-win situation. As you’ve discovered, the VBScript MsgBox function can display a maximum of 1,023 characters; if you have a string larger than that, any characters past the first 1,023 will be cut off. The Wscript.Echo method, by contrast, can display more than 1,023 characters. That’s great, except for one thing: with MsgBox you can change the title of the actual message box, but with Wscript.Echo your message box will always be titled Windows Script Host.

Incidentally, if you want to see what we’re talking about, try running this little test script. The test script creates a string consisting of 1,023 periods followed by the letter X. (In other words, a message that makes absolutely no sense, which puts it on a par with most of these columns.) We then display the message box using both the MsgBox function and the Wscript.Echo method.

Note. Of course, Wscript.Echo displays a message box only when the script is run under Wscript; otherwise the message will be written to a command window.

Here’s the test script:

Set objShell = CreateObject(“Wscript.Shell”)
For i = 1 to 1023
   strMessage = strMessage & “.”
Next
strMessage = strMessage & “X”

MsgBox strMessage Wscript.Echo strMessage

Take a look at the message box that appears when we use the MsgBox function; notice that the X (character number 1,024) doesn’t show up. You might note as well that this message box doesn’t even have a title; that’s because we didn’t give it one:

Message Box


Now here’s the same message displayed using Wscript.Echo. Notice that this time the X (character number 1,024) is displayed; unfortunately, though, we’re stuck with the message box title Windows Script Host:

Message Box


So what’s the answer to our dilemma? As it turns out, the answer is to use the Windows Script Host Popup method. Popup can display as many characters as Wscript.Echo plus it allows us to specify a custom title for our message box. It’s the best of both worlds.

Let’s show you a sample script that uses the Popup method, and then we’ll explain how it works:

Const OK_BUTTON = 0
Const AUTO_DISMISS = 0

Set objShell = CreateObject(“Wscript.Shell”)

For i = 1 to 1023 strMessage = strMessage & “.” Next strMessage = strMessage & “X”

objShell.Popup strMessage, AUTO_DISMISS, “My Message Box”, OK_BUTTON

We begin by defining a pair of constants – OK_BUTTON and AUTO_DISMISS – and set the value of each of these constants to 0. Later on in the script we’ll use OK_BUTTON to indicate that we want a message box that uses only an OK button, and we’ll use AUTO_DISMISS to indicate that we want the message box to remain onscreen until the user clicks OK. Does that mean we could create message boxes that use buttons other than just OK, and that we could create message boxes that can automatically dismiss themselves? You bet it does; for more information, see the Windows Script Host documentation on MSDN.

Next we create an instance of the WSH Shell object, the object that actually contains the Popup method. We then use these lines of code to create our string of 1,023 periods plus the letter X:

For i = 1 to 1023
   strMessage = strMessage & “.”
Next
strMessage = strMessage & “X”

All that’s left now is to call the Popup method and display a message box containing our entire string, a message box that also has the custom title My Message Box:

objShell.Popup strMessage, AUTO_DISMISS, “My Message Box”, OK_BUTTON

As you can see, the Popup method takes four parameters:

•

strMessage, the text we want to display.

•

AUTO_DISMISS, the constant that tells Popup to keep the message box displayed until the user clicks OK.

•

“My Message Box”, the custom title for the message box.

•

OK_BUTTON, the constant that tells Popup to include only an OK button in the message box.

When we run the script we should see a message box that not only displays all 1,024 characters, but has a custom title as well:

Message Box


Just what the doctor ordered.

0 comments

Discussion is closed.

Feedback usabilla icon