I used the following code in word-VBA. But when I executed it on a selected paragraph, it gives Run-time error-5854 "String parameter too long". It works fine for small paragraphs. Please help.
Code:
Sub Count_of_words()
'
' Count Macro
'
'
Dim WrdArray() As String
Dim i As Integer
WrdArray() = Split(Selection, ".") 'Change with ; if required
'MsgBox ("length: " & UBound(WrdArray))
For i = 0 To UBound(WrdArray)
If (Number_of_Words(WrdArray(i))) > "20" Then
'****Set search parameters***************
Selection.Find.Text = WrdArray(i)
Selection.Find.Execute
Selection.Collapse wdCollapseStart
Selection.Select
Selection.MoveRight wdCharacter, Len(WrdArray(i)) + 1, True
Selection.Range.Font.Color = RGB(255, 0, 0)
Selection.Collapse wdCollapseEnd
'****************************************
End If
Next i
End Sub
Function Number_of_Words(Text_String As String) As Integer
'Function counts the number of words in a string
'by looking at each character and seeing whether it is a space or not
Number_of_Words = 1
Dim String_Length As Integer
Dim Current_Character As Integer
Dim actualText As String
actualText = Trim(Text_String)
String_Length = Len(actualText)
For Current_Character = 1 To String_Length
If (Mid(actualText, Current_Character, 1)) = " " Then
Number_of_Words = Number_of_Words + 1
End If
Next Current_Character
'MsgBox ("Number_of_Words: " & Number_of_Words)
End Function
If (Number_of_Words(WrdArray(i))) > "20")
can't be right becauseNumber_of_Words()
returns an int. – Paul Ogilvie