MsgBox hyperlink

D

Dirk Goldgar

Joel Wiseheart said:
Is it possible to put a hyperlink in a MsgBox function
text?

No. You could build and display your own form for the purpose. There
are some complications to deal with if the form is going to be different
every time you display it, so you have to decide if it's worth the
coding effort.
 
C

Cheryl Fischer

If you mean replace the Yes, No, OK, etc., button text with a hyperlink
value, I don't believe you can. In the MsgBox() function, you are limited
to using the numeric button value or its equivalent constant. However, you
could create a small pop-up form with a command button or two and use the
HyperLink Address property of the button to insert the address. When you
enter a string for the button's caption, it will be formatted as a
HyperLink. You could also do some coding to vary what the caption is, I
would think.

hth,
 
Joined
Sep 6, 2011
Messages
2
Reaction score
0
VBA solution:
You can use a form to display a prompt and a clickable link as shown below. This link is nothing else but an underlined blue label associated to a hand mouse icon and with a "Click" event handler on it. The form's height will adapt to the number of lines occupied by the prompt and hyperlink text.

Looks like the following:

attachment.php


Here is the code for the form:

Code:
' UFHyperlink form by Big Cnut
' Displays a message box like form with a clickable link
' Usage: UFHyperlink.HLinkMsgBox "Click on the link below to open it:", "HLinksMsgBox Test", "C:\Temp"

Private HLinkOpenCmd As String  ' The command to use for opening the hyperlink

Private Sub BtnClose_Click()
  Hide
End Sub

Private Sub LHyperlink_Click()
  Shell HLinkOpenCmd & LHyperlink.Caption, vbNormalFocus  ' Open the link
  Hide
End Sub

Private Sub UserForm_Initialize()
  LHyperlink.Font.Underline = True  ' Make it look like a link
End Sub

Public Sub HLinkMsgBox(Prompt As String, Title As String, Hyperlink As String, Optional OpenCmd As String = "Explorer.exe /select, ")
  HLinkOpenCmd = OpenCmd  ' Set the command for opening the hyperlink
  With UFHyperlink
    .Caption = Title  ' Set the window's title
    ' Set the prompt
    With .LPrompt
      .Caption = Prompt  ' Set the width in order to use the whole form's width (as setting the caption resized the label)
      .Width = UFHyperlink.Width - 17.25
      ' Now trigger a vertical resize
      .AutoSize = False
      .AutoSize = True
    End With
    ' Set the hyperlink
    With .LHyperlink
      .Caption = Hyperlink
      .Top = UFHyperlink.LPrompt.Top + UFHyperlink.LPrompt.Height + 4  ' Adjust the vertical position depending on the height of the prompt
      .Width = UFHyperlink.Width - 17.25  ' Set the width in order to use the whole form's width (as setting the caption resized the label)
      ' Now trigger a vertical resize
      .AutoSize = False
      .AutoSize = True
    End With
    .BtnClose.Top = .LHyperlink.Top + .LHyperlink.Height + 10  ' Adjust the vertical position of the close button
    .Height = .BtnClose.Top + .BtnClose.Height + 30  ' Adjust the form's height
    .Show  ' Display the form
  End With
End Sub
You can test it by adding the following sub to a module:

Code:
Sub TESTHLinkMsgBox()
  UFHyperlink.HLinkMsgBox "Click on the link below to open it:", "HLinksMsgBox Test", "C:\Temp"
End Sub
You'll find the form ready for import in the attached zip file, as well as the hand cursor displayed when the mouse moves over the link.

Hope this helps,
Big Cnut.
 

Attachments

  • HLinksMsgBox.png
    HLinksMsgBox.png
    6.3 KB · Views: 4,538
  • UFHyperlink.zip
    2.2 KB · Views: 746

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top