Here are three more word-counting sleighdogs, slightly more refined
but still slobbering. The first one's a little more fun than the
others. This time they skip "smart" quotes too.
The third one, count-all-words-in-doc, took about 5 seconds to find
over 26,000 words and about 10 seconds to find over 45,000 words.
Add characters to skip, as needed, within the MoveEndWhile statement.
'========================================================================
Sub CountUserSpecifiedNumberOfWords()
'COUNT USER-SPECIFIED NUMBER OF WORDS, STARTING AT INSERTION POINT
'SKIP ANYTHING OTHER THAN WORDS OR NUMBERS
If Selection.Type <> wdSelectionIP Then
MsgBox "Quitting.", , "Text Can't Be Selected"
Exit Sub
End If
Dim R As Range, X As Range, WordCnt As Long, UserNumber As Long
UserNumber = InputBox("Number of words:", "Number of Words to Count",
10)
Set R = Selection.Range
Do
R.MoveEndWhile vbCr & vbTab & Chr(160) & Chr(147) & Chr(148) & "
..,;:'()[]!@#$%^&*-_+-={}~`\/?<>|"""""
Set X = R.Duplicate
X.Start = X.End
X.MoveEnd wdWord
X.Select
WordCnt = WordCnt + 1
MsgBox X.Text, , WordCnt
R.MoveEnd wdWord
R.MoveEndWhile vbCr & vbTab & Chr(160) & Chr(147) & Chr(148) & "
..,;:'()[]!@#$%^&*-_+-={}~`\/?<>|"""""
Loop Until WordCnt = UserNumber
Selection.Collapse
End Sub
'========================================================================
Sub CountWordsInSelectedText()
'COUNT WORDS IN BLOCK OF SELECTED TEXT
'SKIP ANYTHING OTHER THAN WORDS OR NUMBERS
If Selection.Type = wdSelectionIP Then
MsgBox "Quitting.", , "Nothing Selected"
Exit Sub
End If
Dim R As Range, WordCnt As Long
Set R = Selection.Range.Duplicate
R.End = R.Start
Do While R.End < Selection.Range.End
R.MoveEndWhile vbCr & vbTab & Chr(160) & Chr(147) & Chr(148) & "
..,;:'()[]!@#$%^&*-_+-={}~`\/?<>|"""""
R.MoveEnd wdWord
WordCnt = WordCnt + 1
R.MoveEndWhile vbCr & vbTab & Chr(160) & Chr(147) & Chr(148) & "
..,;:'()[]!@#$%^&*-_+-={}~`\/?<>|"""""
Loop
MsgBox WordCnt
End Sub
'========================================================================
Sub CountAllWordsInWholeDoc()
'COUNT ALL WORDS IN MAIN STORY RANGE OF DOCUMENT
'SKIP ANYTHING OTHER THAN WORDS OR NUMBERS
Dim R As Range, X As Range, WordCnt As Long
Set X = ActiveDocument.Range.Duplicate
X.End = X.End - 1
Set R = Selection.Range
Do While R.End < X.End
R.MoveEndWhile vbCr & vbTab & Chr(160) & Chr(147) & Chr(148) & "
..,;:'()[]!@#$%^&*-_+-={}~`\/?<>|"""""
R.MoveEnd wdWord
WordCnt = WordCnt + 1
R.MoveEndWhile vbCr & vbTab & Chr(160) & Chr(147) & Chr(148) & "
..,;:'()[]!@#$%^&*-_+-={}~`\/?<>|"""""
Loop
MsgBox WordCnt, , "WordCnt for Whole Doc"
End Sub
Hi, gang
This is a homely old sleighdog of a macro but I'd be curious to see
how else an accurate word count could be arrived at for the following
sentence:
"One + two - three = four," five, 6, seven; "eight nine ten!!!"
Eleven twelve>>thirteen<< fourteen? '"` ~15||| . . .
'sixteen'\.^seventeen``= (eighteen@nineteen%):::::
$twenty???
Code:
(Place cursor before first word.)
Dim R As Range, WordCnt As Integer
Set R = Selection.Range
Do
R.MoveEndWhile vbCr & vbTab & Chr(160) & "
.,;:'()[]!@#$%^&*-_+-={}~`\/?<>|"""""
R.MoveEnd wdWord
R.MoveEndWhile vbCr & vbTab & Chr(160) & "
.,;:'()[]!@#$%^&*-_+={}~`\/?<>|"""""
WordCnt = WordCnt + 1
R.Select
MsgBox WordCnt, , "WordCnt"
Loop Until WordCnt = 20
Selection.Collapse
P.S. Won't work with "smart" quotes or em dashes.
- Bruce
Jay Freedman said:
Hi, Ed,
As a completely different alternative, have a look at the
Selection.Extend method.