Я пытаюсь реализовать код, найденный для Word, который будет проверять предложения и помечать их красным цветом, если они превышают 25 слов (не равны или превышают - просто превышают, поэтому следует пометить 26 или более слов).
Проблема, с которой я столкнулся в этом коде, состоит в том, что он будет помечать предложения короче 25 слов, если вы включите в предложение запятые - это может также случиться с другими знаками препинания, но до сих пор я испытывал этос запятыми конкретно.
Вот код:
Sub AutoExec()
‘ The AutoExec is a special name meaning that the code will run automatically when Word starts
CustomizationContext = NormalTemplate
‘ Create key binding to change the function of the spacebar so that it calls the macro Check_Sentence
‘ each time the spacebar is pressed
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeySpacebar), _
KeyCategory:=wdKeyCategoryMacro, _
Command:=”Check_Sentence”
‘ It will be useful to be able to turn the checking on and off manually
‘ so allocate ctrl-shift-spacebar to turn the checking off
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyShift, wdKeySpacebar), _
KeyCategory:=wdKeyCategoryMacro, _
Command:=”SetSpaceBarOff”
‘ and allocate ctrl-spacebar to turn the checking back on
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeySpacebar), _
KeyCategory:=wdKeyCategoryMacro, _
Command:=”SetSpaceBarOn”
End Sub
Sub SetSpaceBarOn()
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeySpacebar), _
KeyCategory:=wdKeyCategoryMacro, _
Command:=”Check_Sentence”
MsgBox (“sentence length checking turned on”)
End Sub
Sub SetSpaceBarOff()
With FindKey(BuildKeyCode(wdKeySpacebar))
.Disable
End With
MsgBox (“sentence length checking turned off”)
End Sub
Sub Check_Sentence()
Dim long_sentence As Integer
‘ pressing the spacebar calls this macro so have to assume the user wanted a space to appear
‘ in the text. Therefore put a space character into the document
Selection.TypeText (” “)
‘Set number of words to be a long sentence
long_sentence = 25
For Each Test_Sentence In ActiveDocument.Sentences ‘ check each sentence in the document
If Test_Sentence.Words.Count > long_sentence Then ‘ if it longer than our limit
Test_Sentence.Font.Color = wdColorRed ‘ turn the font for the sentence red
‘ Test_Sentence.Font.Underline = wdUnderlineDotted ‘ show long sentences with a dotted underline
Else
Test_Sentence.Font.Color = wdColorBlack ‘ if less than our limit make the font black
‘ Test_Sentence.Font.Underline = wdUnderlineNone ‘ turn of the underline
End If
Next ‘ next sentence
End Sub
Надеясь, что кто-то здесь может подсказать, как пересмотреть код, чтобы избежать этих проблем и помочь ему выполнить его так, как задумано!
Спасибо зазаранее!