Public Function NumberOfWordsChosen(Rch As RichTextBox) As String
Dim pos1 As Integer
Dim pos2 As Integer
Dim txt As String
Dim CurrentWord As String
Dim CurrentChar As String
Dim txtlen As Integer
Dim TmpCount As Integer
TmpCount = 0
pos1 = Rch.SelStart
txt = Rch.Text
Do While pos1 > 1
Do While Mid$(txt, pos1, 1) <> " " _
And Mid$(txt, pos1, 1) <> Chr(10)_
And Mid$(txt, pos1, 1) <> Chr(13)
pos1 = pos1 - 1
If pos1 <1 Then Exit Do
Loop
If pos1 > 1 Then
Do While Mid$(txt, pos1, 1) = " " _
Or Mid$(txt, pos1, 1) = Chr(10)_
Or Mid$(txt, pos1, 1) = Chr(13)
pos1 = pos1 + 1
Loop
End If
If pos1 >= Rch.SelStart + Rch.SelLength Then
NumberOfWordsChosen = TmpCount
Exit Function
End If
pos2 = pos1
txtlen = Len(txt)
Do While Mid$(txt, pos2, 1) <> " " _
And Mid$(txt, pos2, 1) <> Chr(10)_
And Mid$(txt, pos2, 1) <> Chr(13)
pos2 = pos2 + 1
If pos2 > txtlen Then Exit Do
Loop
TmpCount = TmpCount + 1
If pos2 <= Rch.SelStart + Rch.SelLength Then
pos1 = pos2 + 1
While Mid$(txt, pos1, 1) = Chr(10)_
Or Mid$(txt, pos1, 1) = Chr(13)_
Or Mid$(txt, pos1, 1) = " "
pos1 = pos1 + 1
Wend
Else
Exit Do
End If
Loop
NumberOfWordsChosen = TmpCount
End Function