VB Code:
  1. Private Const WM_USER As Long = &H400
  2. Private Const EM_AUTOURLDETECT As Long = (WM_USER + 91)
  3. Private Const EM_GETSEL As Long = &HB0
  4.  
  5. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
  6.      ByVal hwnd As Long, _
  7.      ByVal wMsg As Long, _
  8.      ByVal wParam As Long, _
  9.      lParam As Any) As Long
  10.  
  11. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
  12.      ByVal hwnd As Long, _
  13.      ByVal lpOperation As String, _
  14.      ByVal lpFile As String, _
  15.      ByVal lpParameters As String, _
  16.      ByVal lpDirectory As String, _
  17.      ByVal nShowCmd As Long) As Long
  18.  
  19.  
  20. Option Explicit
  21.  
  22. Private Sub DetectURL(p_RichText As Object, p_blnDetect As Boolean)
  23.     Dim lngRet As Long
  24.     Dim strText As String
  25.    
  26.     With p_RichText
  27.         ' this line is needed because the function will not update the
  28.         ' url if you had it before
  29.         strText = .Text
  30.         ' send message to detect urls
  31.         ' notice the Abs function. This is needed to pass 0 or 1
  32.         ' in VB true is -1, so we have to get the absolute value of that
  33.         lngRet = SendMessage(RichTextBox1.hwnd, EM_AUTOURLDETECT, Abs(p_blnDetect), ByVal 0)
  34.         ' rewrite the text into the RichText so it will change all URLs if you
  35.         'had them before
  36.         .Text = strText
  37.     End With
  38. End Sub
  39.  
  40. Private Sub Form_Load()
  41.  
  42.     RichTextBox1.Text = "The URL to click is [url]http://www.something.com.[/url]  Please click it." & vbCrLf
  43.    
  44. End Sub
  45.  
  46. Private Sub RichTextBox1_Change()
  47.     DetectURL RichTextBox1, True
  48.     RichTextBox1.SelStart = Len(RichTextBox1.Text)
  49. End Sub
  50.  
  51.  
  52. Private Sub RichTextBox1_Click()
  53.  
  54.     Dim lngRetVal As Long
  55.    
  56.     lngRetVal = SendMessage(RichTextBox1.hwnd, EM_GETSEL, 0, 0)
  57.    
  58.     Dim strBuffer As String, intInStr As Integer, intHi As Integer, intLo As Integer
  59.    
  60.     intHi = HiWord(lngRetVal) + 1
  61.     intLo = LoWord(lngRetVal) + 1
  62.    
  63.     intInStr = InStrRev(RichTextBox1.Text, " ", intLo)
  64.    
  65.     If intInStr = 0 Then 'no space
  66.         strBuffer = Mid(RichTextBox1.Text, 1, intLo)
  67.     Else
  68.         strBuffer = Mid(RichTextBox1.Text, intInStr + 1)
  69.     End If
  70.    
  71.     strBuffer = Trim(strBuffer)
  72.     intInStr = InStr(1, strBuffer, " ")
  73.    
  74.     If intInStr <> 0 Then
  75.         strBuffer = Mid(strBuffer, 1, intInStr - 1)
  76.     End If
  77.    
  78.     If InStr(1, strBuffer, "http:") = 0 And _
  79.         InStr(1, strBuffer, "file:") = 0 And _
  80.         InStr(1, strBuffer, "mailto:") = 0 And _
  81.         InStr(1, strBuffer, "ftp:") = 0 And _
  82.         InStr(1, strBuffer, "https:") = 0 And _
  83.         InStr(1, strBuffer, "gopher:") = 0 And _
  84.         InStr(1, strBuffer, "nntp:") = 0 And _
  85.         InStr(1, strBuffer, "prospero:") = 0 And _
  86.         InStr(1, strBuffer, "telnet:") = 0 And _
  87.         InStr(1, strBuffer, "news:") = 0 And _
  88.         InStr(1, strBuffer, "wais:") = 0 Then Exit Sub
  89.        
  90.     Debug.Print strBuffer
  91.    
  92.     'to run
  93.     'Call ShellExecute(Me.hwnd, "OPEN", strBuffer, vbNullString, vbNullString, 5)
  94.    
  95. End Sub
  96.  
  97. Private Function LoWord(ByVal DWord As Long) As Long
  98.   If DWord And &H8000& Then
  99.     LoWord = DWord Or &HFFFF0000
  100.   Else
  101.     LoWord = DWord And &HFFFF&
  102.   End If
  103. End Function
  104.  
  105. Private Function HiWord(ByVal DWord As Long) As Long
  106.   HiWord = (DWord And &HFFFF0000) \ &H10000
  107. End Function