In writing blog articles for our virtual receptionist service I try to keep the sentence length short. There are plenty of experts who say a sentence length of less than 25 words makes it easier to read. Microsoft Word has various tools for showing grammar and can show the average sentence length. It also have a check for sentences over 60 words long, but that is far too long to be useful.
At Parent’s evening for my son who is taking GCSE English this summer the teacher commented that his sentences were too long. I started thinking there must be a way of showing him a sentence is too long when he uses Word.
I set about finding a way to make Word change the font colour automatically as he types once the sentence is over 25 words long, like this sentence.
I am familiar with writing macros for Excel so I looked at ways to do this in Word. The result is the code below which does the following:
- the code is active as soon as you open Word
- when typing a sentence it checks the length every time you hit the spacebar
- if the sentence is over 25 words long the font for the sentence turns red
- if you reduce the number of words the sentence turns black
- you can change the 25 word length to anything you want
- you can turn the checking on and off if need be
Step by Step Instructions
If you have not used macros in Word or Excel this may look a bit scary but just follow these steps one by one.
- 1. In the code section below click on the Copy button in the toolbar. This copies all the code into your clipboard.
- 2. Open Word
- 3. Hold down the Alt key and press F11
- 4. In the window that appears right click on Normal (highlighted in yellow in the screenshot) and Insert Module
- 5. Click in the main editor window and paste (ctrl-v) the code you copied in step 1. It should look like the screenshot below
- 6. Hit the Save icon, circled red in the screenshot
- 7. Close the window using the X in the top right corner.
Code to copy
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
Test the long sentence length checker
Back in your word document try typing a long sentence to see if it turns red once you have more than 25 words.
Sentence Checking – Manually Turn On and Off
To manually turn off the checking hold down Ctrl and shift buttons and press the spacebar.
To turn the checking back on hold down just the Ctrl button and press the spacebar.
Dotted Underline instead of Font Colour
To show long sentences with a dotted underline instead of changing to red font you need to make a simple edit to the code.
Press Alt-F11 to bring up the code window and double click on the module you added under the Normal section. Comment out the line with Font.Colour and remove the comment in the line below. Do the same below the Else statement so the code looks like the hightlighted section in the screenshot below.
If you want to experiment with different types of underline change the wdUnderLineDotted to be one of the names Word supports. See here for a list WdUnderLine parameters
Change the length of a long Sentence
To change the length for a long sentence edit the code (press Alt-F11 as described above) and change the value in the line long_sentence = 25 (arrowed in screenshot below).
Want to remove the code
If you do not like the way it operates and want to remove it the steps are:
- Hold down the Alt key and press F11
- Click on the + signs for Normal and modules
- Right click on the module that has the code (probably Module1) and Remove Module, say No when it asks if you want to export it
- Press the save icon
- Close the code window and Word will operate as normal.
Summary
Hope this is the type of function you were looking for and that the instructions help you to get it working.
Ian
Office Answers
Virtual Office services for small businesses
Latest posts by Ian Marshall. (see all)
- Don’t give up the day job - January 22, 2018
- Self Employment – Pros and Cons - October 9, 2017
- All the benefits of co-working without sacrificing client contact - April 4, 2017