Public Sub HighLight () Sub HighLight (RTB As RichTextBox, lColor As Long)
'Add new color to color table
'Add tags \ highlight # and \ highlight0
'Where # is new color number
Dim iPos As Long
Dim strRTF As String
Dim bkColor As Integer
With RTB
IPos =. SelStart
'Bracket selection
. SelText = Chr (& H9D) &. SelText & Chr (& H81)
StrRTF = RTB. TextRTF
'Add new color
BkColor = AddColorToTable (strRTF, lColor)
'Add highlighting
StrRTF = Replace (strRTF, "\ '9d", "\ up1 \ highlight" & CStr (bkColor )&"")
StrRTF = Replace (strRTF, "\ '81", "\ highlight0 \ up0 ")
. TextRTF = strRTF
. SelStart = iPos
End
End Sub
Function AddColorToTable () Function AddColorToTable (strRTF As String, lColor As Long) As Integer
Dim iPos As Long, jpos As Long
Dim ctbl As String
Dim tagColors
Dim nColors As Integer
Dim tagNew As String
Dim I As Integer
Dim iLen As Integer
Dim split1 As String
Dim split2 As String
'Make new color into tag
TagNew = "\ red" & CStr (lColor And & HFF )&_
"\ Green" & CStr (Int (lColor/& H100) And & HFF )&_
"\ Blue" & CStr (Int (lColor/& H10000 ))
'Find colortable
IPos = InStr (strRTF, "{\ colortbl ")
If iPos> 0 then' if table already exists
Jpos = InStr (iPos, strRTF ,";}")
'Color table
Ctbl = Mid (strRTF, iPos + 12, jpos-iPos-12)
'Array of color tags
TagColors = Split (ctbl ,";")
NColors = UBound (tagColors) + 2
'See if our color already exists in table
For I = 0 To UBound (tagColors)
If tagColors (I) = tagNew Then
AddColorToTable = I + 1
Exit Function
End If
Next I
'{\ Fonttbl {\ f0 \ fnil \ fcharset0 Verdana ;}}
'{\ Colortbl; \ red0 \ green0 \ blue0; \ red128 \ green0 \ blue255 ;}
Split1 = Left (strRTF, jpos)
Split2 = Mid (strRTF, jpos + 1)
StrRTF = split1 & tagNew & ";" & split2
AddColorToTable = nColors
Else
'Color table doesn' t exists, let's make one
IPos = InStr (strRTF, "{\ fonttbl") 'ning inning of font table
Jpos = InStr (iPos, strRTF, ";}}") + 2' end of font table
Split1 = Left (strRTF, jpos)
Split2 = Mid (strRTF, jpos + 1)
StrRTF = split1 & "{\ colortbl;" & tagNew & ";}" & split2
AddColorToTable = 1
End If
End Function