How Can I Display a Hyperlink in a Message Box?

How Can I Display a Hyperlink in a Message Box?

  • Comments 3
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! Is it possible to have a hyperlink to a Web page in a message box?

-- CB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CB. Sounds like you want to display a message box to users and, ideally, you’d like to include a hyperlink they could click on for more information. For example, suppose a user tried to access some resource and was turned away. In a case like that, you could pop up a message box that tells them that access is denied, then provide them with a link to a Web page that tells them what they need to do to gain access. All in all a much nicer solution than simply telling them “Access Denied.”

So can you do this using a scripting language? Well, no, not as far as we know. Hyperlinks in message boxes would be very cool, but you’re pretty much limited to rudimentary message boxes when using a scripting language.

But wait, don’t leave! You didn’t ask us, “Is there a workaround of some kind, something that does the same thing but doesn’t embed a hyperlink in a message box?” Had you asked us that, we would have said, “Hey, there’s always a workaround of some kind.” What you’re looking for is one-click access to a Web page from a message box. One way to do that would be to embed a hyperlink in the message box; unfortunately, as we just noted, you’re not going to do that with VBScript. So, let’s try another way: why not pop up a message box that asks, “Do you want to apply for access to this resource?” If the user clicks No, the script terminates. If the user clicks Yes, then the script automatically takes them to the appropriate Web page. One message box, one-click access to a Web site. It’s not quite the same as using a hyperlink, but the net result is identical.

As you can see, this only takes a few lines of code:

Set objShell = CreateObject("Wscript.Shell")

intMessage = Msgbox("Would you like to apply for access to this resource?", _
    vbYesNo, "Access Denied")

If intMessage = vbYes Then
    objShell.Run("http://www.microsoft.com")
Else
    Wscript.Quit
End If

We begin by creating an instance of the WSH Shell object; we’ll need that in order to pop up a Web browser and connect to the Web site. We then display a message box (with the title Access Denied) that asks “Would you like to apply for access to this resource?” This message box has a Yes button and a No button, that’s what the VBScript constant vbYesNo is there for.

Note. Some of you might be thinking, “Wait, I thought you had to explicitly define constants in VBScript.” That’s true in most cases; if we were working with the FileSystemObject, for example, we’d need to define constants using code like this:

Const ForReading = 1

However, VBScript has some intrinsic constants of its own. vbYesNo is one; it’s used to display a Yes button and a No button in a message box. vbCrLf is another; it’s used to append a carriage return-linefeed to the end of a string. These intrinsic constants - which are part of the VBScript language - don’t have to be defined; VBScript knows what you mean when you type vbYesNo.

Now, where were we? Ah, yes, we pop up a message box and give the user Yes and No buttons. If the user clicks Yes, we’re going to take them to the specified Web site. How do we know if they clicked Yes? That’s easy: when we create the message box, we store the user’s response in the variable intMessage. To determine which button the user clicked, we just check the value of intMessage. If the value is equal to vbYesNo (another intrinsic constant, equal to 6) then the user clicked Yes, and we use the Shell object’s Run method to open the Web site http://www.microsoft.com. (Notice that all we have to do is specify the URL; the operating system then use the default browser to navigate to that site.)

And what if the user clicks No? In that case, we just quit (Wscript.Quit).

Like we said, not exactly what you had in mind, but it’ll do the trick.

Incidentally, if you’d like to know more about VBScript’s Msgbox function and the different ways you can configure it, check out this portion of the VBScript Language Reference on MSDN.

And what if you have a need to create really elaborate message boxes? Well, in that case, you could actually make yourself an HTML page and use that to mimic a true Windows message box. But that’s a story we’ll have to tell some other day.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks a lot. IT WORKS!

  • Excellent stuff, been looking to get this working for quiet some time!

  • Great workaround, but pressing either Yes or No has the same result of opening the URL.   Still good for me, though.