Library code snippets

Colour HTML Tags (2)

This is another example of how to colour HTML tags. Add a RichTextBox to your form, and a command button called cmdColour. Then use the following code


Private Sub ColorHTML(RTF As RichTextBox)
  
   Dim sHTML      As String
   Dim nTagOpen   As Long
   Dim nTagClose  As Long
   Dim nI         As Long
  
   Dim nColor     As Long
  
   sHTML = RTF.Text
   nTagClose = 1
  
   With RTF
      For nI = 1 To Len(sHTML)
         ' *** See where the next tag starts.
         nTagOpen = InStr(nTagClose, sHTML, "<")
         If nTagOpen = 0 Then Exit For
        
         If LCase(Mid(sHTML, nTagOpen, 4)) = "<img" Then
            nColor = &H400040
         ElseIf LCase(Mid(sHTML, nTagOpen, 2)) = "<a" Then
            nColor = &H8080&
         ElseIf LCase(Mid(sHTML, nTagOpen, 2)) = "<b" Then
            nColor = &H8000000F
         Else
            nColor = &H4000&
         End If
        
         ' *** See where the tag ends.
         nTagClose = InStr(nTagOpen, sHTML, ">")
         If nTagOpen = 0 Then nTagClose = Len(sHTML)
         If nTagClose = 0 Then nTagClose = Len(sHTML)
         ' *** Color the tag.
         .SelStart = nTagOpen - 1
         .SelLength = nTagClose - nTagOpen + 1
         .SelColor = nColor
      Next
   End With
  
End Sub

Private Sub cmdColour_Click()
    ColorHTML RichTextBox1
End Sub

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Colour HTML Tags (2).

Leave a comment

Sign in or Join us (it's free).