by Vicky Jadhav (Vj) (8 Submissions)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Tue 12th October 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Count number of words
'and Follow the following codes given,,,
Private Sub Command1_Click()
Dim position As Long
Dim words As Long
Dim myText As String
position = 1
myText = Text1.Text
' massage string:
' replace line feeds with spaces
myText = Replace(myText, Chr(13) & Chr(10), " ")
' replace tabs with single spaces
myText = Replace(myText, Chr(9), " ")
myText = Trim(myText)
' Count the first word
' Because the last word isn't delimited by
' a space, if the string isn't blank, then it
' contains at least one word.
' By setting words=1, we won't have to increase the
' number of words by 1 when we are done counting.
If Len(myText) > 0 Then words = 1
' while the string contains spaces...
Do While position > 0
position = InStr(position, myText, " ")
' ... increase word count
If position > 0 Then
words = words + 1
' and skip additional spaces
While Mid(myText, position, 1) = " "
position = position + 1
Wend
End If
Loop
MsgBox "The TextBox contains " & words & " words"
End Sub