word counts diff in Word and macro

E

eugene

Hi,

Does anyone know how to count special characters within a range?

A macro that includes ActiveDocument.Words.count gives a different result
for count than Word's word count toolbar. The latter produces the right
result. The macro seems to be including the "paragraph mark" and other items
such as "*" "%" etc. as separate words even though I have them directly
adjacent to words.

I would like to make the count more precise and thus would like to subtract
the number of times these special characters appear in the document and
range.
I can count these special characters in the entire document by looping
through ActiveDocument.Characters and selecting those that have ascii values
equal to, less than or greater than certain values (see code after my
signature). But I don't know how to do this within a range and a macro I am
working on needs to have that ability.

Any help would be appreciated. It will probably be enough to just give me
the equivalent of ActiveDocument.Characters for a loop through a range.

--
eugene

PS Can anyone influence Microsoft to fix this. It is quite odd for there to
be such a difference and is quite disturbing that the macro can't do it right.

CODE
Doug Robbins response to "Count Uppercase alphabets" posted 1/3/07
Dim i As Long, achar As Range
i = 0
For Each achar In ActiveDocument.Characters
If Asc(achar) > 64 Then
If Asc(achar) < 91 Then
i = i + 1
End If
End If
Next achar
MsgBox i
 
G

Greg Maxey

Eugene,

To do something with a range, you declare it "Dim", define it "Set", use it,
redefine it (if you want), then kill it (I usually forget this part).

Sub EugenesRangeCount()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Paragraphs(1).Range
MsgBox oRng.Words.Count
Set oRng = Selection.Range
MsgBox oRng.Words.Count
Set oRng = oRng.Sentences(1)
MsgBox oRng.Words.Count
Set oRng = Nothing
End Sub

There are other ways to count words:

Sub ScratchMacro()
Dim msg As String
msg = "From straight VBA Word.Count = " &
ActiveDocument.Range.Words.Count
msg = msg + vbCr & "From Word Count dialog = " & DlgWordCount
msg = msg + vbCr & "From ReadabilityStats = " & RS

MsgBox msg
End Sub
Private Function DlgWordCount() As Long
Dim dlg As Dialog
Set dlg = Dialogs(wdDialogToolsWordCount)
dlg.Execute
DlgWordCount = dlg.Words
Set dlg = Nothing
End Function
Private Function RS() As Long
Dim oRg As Range
If Selection.Type = wdSelectionIP Then
Set oRg = ActiveDocument.Range
Else
Set oRg = Selection.Range
End If
RS = oRg.ReadabilityStatistics("Words").Value
Set oRg = Nothing
End Function
 
E

eugene

Greg,

As usual your suggestions are extremely helpful.
Your code (previous post and below) has:
msg = "From straight VBA Word.Count = " & ActiveDocument.Range.Words.count
msg = msg + vbCr & "From Word Count dialog = " & DlgWordCount

The second line works right and is perfect for my needs. But what is
disturbing is that the first line does not give the same count as the second.
(Change all spaces to ^p and you get entirely different results between 1 and
2. #1 includes ^p as a "word," which is certainly not what most would expect
or want.)

I am having problems with the "ReadabilityStatistics" part of the code. Went
to "help" and did not help. But line 2 above is enough so no need for further
help on this.

BTW - looked at the website (link below). May be able to use some items
and/or links. Good job! and thanks again.
 
G

Greg Maxey

Eugene,

Please to assists. Yes, while it might not have been evident with the
little illustration I provided, I realized that straigt Range.Words.Count
return a confussing and rather useless value. I don't know why that is.
 

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