VBA count of mis-spelled words?

K

KR

How can I get a count of mis-spelled words in a document, using VBA?
Basically, the number of words with the red squiggly underline, although
related errors like mixed capitalization would also be of interest
especially if I could keep that count separate from the basic count of
mis-spelled words.

Thanks!
 
J

Jonathan West

KR said:
How can I get a count of mis-spelled words in a document, using VBA?
Basically, the number of words with the red squiggly underline, although
related errors like mixed capitalization would also be of interest
especially if I could keep that count separate from the basic count of
mis-spelled words.

Thanks!


ActiveDocument.Range.SpellingErrors.Count returns the number of spelling
errors in the body of the document.

If you look up SpellingErrors in the VBA Help file, you will see what else
you might be able to do. For instance, the SpellingErrors object is a
collection of Range objects, so you can iterate through the collection and
get the text of each individual spelling error.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
S

Shauna Kelly

Hi KR

Look up SpellingErrors in the VBA help.

From the help file:

myErr = ActiveDocument.SpellingErrors.Count
If myErr = 0 Then
Msgbox "No spelling errors found."
Else
Msgbox myErr & " spelling errors found."
End If

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
K

KR

Jonathan and Shauna-

Thank you for your reply- I'm usually in Excel VBA, and sometimes it takes a
whack with the obvious when I switch applications ;-)

Based on the spelling errors count (and some quick testing), it appears that
if the user is in the middle of typing a word when this code is fired, it
will count an extra error for the half-spelled word (unless the half
represents a different word in the dictionary).

I took a quick search using "range" as a keyword, but didn't see anything
that would easily explain how to switch the range from the whole document,
to everything minus the last word in the document (if this is even the best
approach?). I'll be doing this check without alerting or interrupting the
user, so I'd like to avoid having the range calculation affect the cursor
position in the document.

Many thanks,
Keith
 
J

Jonathan West

KR said:
Jonathan and Shauna-

Thank you for your reply- I'm usually in Excel VBA, and sometimes it takes
a
whack with the obvious when I switch applications ;-)

Based on the spelling errors count (and some quick testing), it appears
that
if the user is in the middle of typing a word when this code is fired, it
will count an extra error for the half-spelled word (unless the half
represents a different word in the dictionary).

I took a quick search using "range" as a keyword, but didn't see anything
that would easily explain how to switch the range from the whole document,
to everything minus the last word in the document (if this is even the
best
approach?). I'll be doing this check without alerting or interrupting the
user, so I'd like to avoid having the range calculation affect the cursor
position in the document.

Something like this should do

Function SpellingErrorCount() As Long
Dim oRange as Range
Set oRange = ActiveDocument.Range
oRange.MoveEnd Unit:=wdWord, Count:=-1
SpellingErrorCount = oRange.SpellingErrors.Count
End Function


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top