Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
String) As Long
Private Const EM_GETLINE = &HC4
Private Const EM_LINELENGTH = &HC1
Private Function ColorWords(ByRef rtb As RichTextBox, _
ByRef sFindString As String, _
ByVal lColor As Long) As Integer
On Error GoTo ErrHandler
Dim lFindLength As Long
Dim lFoundPos As Long
Dim lTempPos As Long
Dim iOptions As Integer
Dim iMatchCount As Integer
' Set search options
iOptions = rtfNoHighlight 'used by default here
If Check1(0).Value = 1 Then iOptions = iOptions + rtfWholeWord
If Check1(1).Value = 1 Then iOptions = iOptions + rtfMatchCase
' Save the string-length
lFindLength = Len(sFindString)
' Search for a single match. Find method returns
' the position of the first character of the item,
' or -1 if no matches are found.
lFoundPos = rtb.Find(sFindString, 0, , iOptions)
' Loop until all occurences are found
Do Until lFoundPos < 0
iMatchCount = iMatchCount + 1
rtb.SelStart = lFoundPos
rtb.SelLength = lFindLength
rtb.SelColor = lColor
lTempPos = lFoundPos + lFindLength
lFoundPos = rtb.Find(sFindString, lTempPos, , iOptions)
Loop
' Return the number of matches
ColorWords = iMatchCount
Exit Function
ErrHandler:
MsgBox "Unexpected error number-" & _
CStr(Err.Number) & _
" occurred in " & Err.Source & _
":" & vbCrLf & vbCrLf & Err.Description
End Function
Private Sub Command1_Click()
ColorWords RichTextBox1, txtSearch.Text, vbRed
End Sub
Private Sub RichTextBox1_Click()
Dim strBuffer As String
Dim lngLength As Long
Dim intCurrentLine As Integer
Dim lngLineNumber As Long
With RichTextBox1
intCurrentLine = .GetLineFromChar(.SelStart)
'get line length
lngLength = SendMessage(.hwnd, EM_LINELENGTH, intCurrentLine, 0)
'resize buffer
strBuffer = Space(lngLength)
'get line text
Call SendMessage(.hwnd, EM_GETLINE, intCurrentLine, ByVal strBuffer)
lngLineNumber = .GetLineFromChar(.SelStart)
MsgBox "You selected line number " & lngLineNumber + 1 & " which
says " & strBuffer
End With
End Sub

Signature
David Glienna
MVP - Visual Developer (Visual Basic)
2006 thru 2008
> How to control size of length in richtexbox per line ?
>
> Regards,
> Tee