Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-04-2018, 06:09 AM
Nanaia Nanaia is offline Sentence word count Windows XP Sentence word count Office 2013
Novice
Sentence word count
 
Join Date: Dec 2016
Location: Lexington, KY
Posts: 10
Nanaia is on a distinguished road
Default Sentence word count


I'm trying to create a macro that will highlight any words of a sentence that is over 21 words. The goal is to make sure I keep sentences under 22 words. I found a macro online but I get a "Compile error: Syntax error" when I try to run it. I've just began learning macros so I'm not sure how to resolve the error. This is the code I found and am getting an error on:
Code:
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 = 21
  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

Last edited by macropod; 09-04-2018 at 02:10 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 09-04-2018, 02:12 PM
macropod's Avatar
macropod macropod is offline Sentence word count Windows 7 64bit Sentence word count Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Before your code will have a chance of running, you'll need to replace all those 'smart' single quotes and double quotes with straight quotes. Even then, however, you should be aware that VBA has no idea what a grammatical sentence is. For example, consider the following:
Quote:
Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.
For you and me, that would count as one sentence; for VBA it counts as 5 sentences.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-05-2018, 03:47 AM
Nanaia Nanaia is offline Sentence word count Windows 7 64bit Sentence word count Office 2016
Novice
Sentence word count
 
Join Date: Dec 2016
Location: Lexington, KY
Posts: 10
Nanaia is on a distinguished road
Default

Macropod/Paul,
Thank you for the information. I have no issues with a macro thinking your example is 5 sentences. I understand that it doesn't know the difference between a period at the end of a sentence and one used in a title such as mister (Mr.) or a dollar value and I'm okay with that.
When I try to change the 'smart' single quotes (') with straight quotes (") I get the following error

Compile error:
Expected: line number or label or statement or end of statement

with an OK and a HELP button. when I select the HELP button it opens a web browser page. I've attached a screen shot. If there are recommendations for a different macro I am open to new ideas.
Thank you.
Attached Files
File Type: zip Untitled.zip (518.7 KB, 16 views)
Reply With Quote
  #4  
Old 09-05-2018, 05:56 AM
macropod's Avatar
macropod macropod is offline Sentence word count Windows 7 64bit Sentence word count Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

All those red lines in your code are errors. Instead of replacing 'smart' single quotes with straight single quotes and replacing 'smart' double quotes with straight double quotes, all you've done is to replace some 'smart' single quotes with straight double quotes.

In the posted code, there are 18 'smart' single quotes to replace with straight single quotes and 14 'smart' double quotes to replace with straight double quotes.

You said in post #1:
Quote:
The goal is to make sure I keep sentences under 22 words
and now you say you're OK with VBA's inaccuracies in the grammatical sentence Word count. That's illogical: the demo sentence I posted has 26 words...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 09-05-2018, 07:04 AM
Nanaia Nanaia is offline Sentence word count Windows 7 64bit Sentence word count Office 2016
Novice
Sentence word count
 
Join Date: Dec 2016
Location: Lexington, KY
Posts: 10
Nanaia is on a distinguished road
Default

You may feel my goal is illogical, however all I want to do is have the sentence flagged. I don't use titles or dollar values in my papers so worrying about those breaking up my sentence word count won't be an issue. I also understand that hyphens and other punctuations can affect it. If I am ever in the situation where I actually do use titles or dollar values or anything that places punctuation in the middle of a sentence and not used to end the sentence I will be hyper vigilant and highlight those sentences to get an accurate word count. In the meantime I just wanted something that will help draw my attention to my sentences that are too long.
You recommendation worked.
Thank you for your time and assistance! It is greatly appreciated!

Reply With Quote
Reply

Tags
vba code

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sentence word count Split sentence at bold word jeffreybrown Word 2 08-17-2018 07:09 PM
Sentence word count How to count alphabets in a sentence? Awesomeg Word 2 03-09-2018 09:37 PM
Sentence word count Delete does not bring second sentence closer to first sentence Andoheb Word 29 07-03-2014 01:48 PM
Sentence word count First word of sentence IF and then awolf Word VBA 7 03-16-2014 02:40 PM
I want to lock a sentence in Word smorkette Word 1 02-06-2010 12:57 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:32 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft