Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ

VB Component Trick

How to make a hyperlink using a Rich Text Box by Scoty
Posted: 12 Mar 04 (Edited 15 Mar 04)

One day our support staff came to me and asked if there was anyway they could add hyperlinks to an application that I wrote that utilized a rich text box component.  I have had my troubles with hyperlinks before but I thought I would just give it a try.  Here is what I came up with.  It is a simple click function that returns the hyperlink string and then shells it into IExplore.  I hope you find it useful.  Give me some feed back

You will need to start a new project then add the RichTextBox component.  Drop a RTB on the form anywhere

then add this code


Private Sub Form_Load()

GetThisHyperLink = InputBox("HyperLink?", "Please enter a WebPage", "http:\\www.tek-tips.com")
GetThisHyperLink = Replace(GetThisHyperLink, "\\", "\\\\", 1)

Me.RichTextBox1 = "{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}" & Chr(13) & _
                    "{\colortbl ;\red0\green0\blue255;} " & Chr(13) & _
                    "\viewkind4\uc1\pard\lang1033\f0\fs17 This would be the non hyperlink beginning of the richtext box \cf1\ul " & GetThisHyperLink & "\cf0\ulnone  and this would be the end of the non hyperlink text" & Chr(13) & _
                    "\par }"

End Sub

Private Sub RichTextBox1_Click()
With Me.RichTextBox1
    If .SelColor = vbBlue And .SelUnderline Then
        'find the beginning of the Hyperlink
        For I = .SelStart To 1 Step -1
            .SelStart = I
            If .SelStart = I then 'sometimes the .SelStart doesn't change..this is the work around
                If Not .SelUnderline Then
                    Exit For
                End If
            End If
        Next I
        StartPos = .SelStart
        'Find the end of the Hyperlink
        For I = .SelStart + 1 To Len(Me.RichTextBox1)
            .SelStart = I
            If .SelStart = I then 'sometimes the .SelStart doesn't change..this is the work around
                If Not .SelUnderline Then
                    Exit For
                End If
            End If
        Next I
        EndPos = .SelStart - 1
        'Now Scrape HyperLink
        .SelStart = StartPos
        .SelLength = EndPos - StartPos
        MyHyperLink = .SelText
        .SelStart = 1
        .SelLength = 0
        'Now launch internet explorer to that page
        Shell """C:\Program Files\Internet Explorer\IExplore.exe """ & MyHyperLink, vbNormalFocus
    End If
End With
End Sub

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close