我在word-VBA中使用了以下代码。但是当我在选定的段落上执行它时,它会给出运行时错误-5854“字符串参数太长”。它适用于小段落。请帮忙。
代码:
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
答案 0 :(得分:0)
您还没有告诉我们 错误发生在哪里。
在网上进行一些研究,似乎Find
和Replace
功能仅限于255个字符的字符串。
因此,调用此功能时似乎发生错误,而不是在将字符串传递给Number_of_Words()
时发生错误。
答案 1 :(得分:0)
该代码似乎非常错误,因为它假设所有句子都以“。”分隔。并且所有单词都用一个“”分隔。我认为它应该是这样的:
Dim s As Range
For Each s In Selection.Sentences
If s.Words.Count > 20 Then s.Font.Color = wdColorRed
Next