Hyperlinks in Userforms

In the userform example shown in Figure 21.4, there is a field for email and a Web site. Wouldn't it be nice to be able to click on these and have a blank email message or Web page automatically appear? You can! The following program creates a new message or opens a Web browser when the corresponding label is clicked:

The API declaration, and any other constants, go at the very top of the code.

Private Declare Function ShellExecute Lib "shell32.dll" Alias _

"ShellExecuteA"(ByVal hWnd As Long, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As String, _ ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Const SWNormal = 1

This sub controls what happens when the email label is clicked, as shown in Figure 21.7.

Private Sub lbl_Email_Click() Dim lngRow As Long lngRow = TabStripl.Value + 1

ShellExecute 0&, "open", "mailto:" & Cells(lngRow, 5).Value, _ vbNullString, vbNullString, SWNormal

End Sub

This sub controls what happens when the Web site label is clicked.

Private Sub lbl_Website_Click() Dim lngRow As Long lngRow = TabStripl.Value + 1

ShellExecute 0&, "open", Cells(lngRow, 6).Value, vbNullString, _ vbNullString, SWNormal

Figure 21.7

Turn email addresses and Web sites into clickable links.

Figure 21.7

Turn email addresses and Web sites into clickable links.

0 0

Post a comment