Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
date: Wed, 20 Feb 2008 15:35:53 -0000,    group: microsoft.public.word.vba.beginners        back       


VBA Word Count   
Hi, couple of minor problems. I wanted to help my wife with a macro that 
would count all the words in the document and also the number of words in 
bold. I can't even get to first base on this because the VBA word count 
seems to be all screwy.

For example: 'MsgBox ActiveDocument.Range.Words.Count' returns an answer of 
10 for a document that has 5 words in it. The word count on the menu 
Tools...word count gives the correct answer. Is there a simple way to access 
that, and why does words.count not just ummm count the words?

BTW this is Word 2003, and it's up to date with all the patches.
date: Wed, 20 Feb 2008 15:35:53 -0000   author:   GB

Re: VBA Word Count   
GB wrote:
> Hi, couple of minor problems. I wanted to help my wife with a macro
> that would count all the words in the document and also the number of
> words in bold. I can't even get to first base on this because the VBA
> word count seems to be all screwy.
>
> For example: 'MsgBox ActiveDocument.Range.Words.Count' returns an
> answer of 10 for a document that has 5 words in it. The word count on
> the menu Tools...word count gives the correct answer. Is there a
> simple way to access that, and why does words.count not just ummm
> count the words?
> BTW this is Word 2003, and it's up to date with all the patches.

The Words collection contains a separate item for each punctuation mark, 
each paragraph mark, and each inline graphic in the document. That makes it 
unsuitable for your purpose. The number in the Word Count dialog, which 
matches the usual English definition of "words", is instead available from 
the ComputeStatistics method.

Here's sample code that you can modify to your taste:

Sub demo()
    Dim WordCount As Long
    Dim myRange As Range

    Set myRange = ActiveDocument.Range

    WordCount = myRange.ComputeStatistics(wdStatisticWords)
    MsgBox WordCount & " words"

    ' now count bold words
    WordCount = 0
    With myRange.Find
        .ClearFormatting
        .Format = True
        .Text = ""
        .Font.Bold = True
        .Forward = True
        .Wrap = wdFindStop
        While .Execute
            WordCount = WordCount + 1
            myRange.Collapse wdCollapseEnd
        Wend
    End With
    MsgBox WordCount & " bold words"
End Sub


-- 
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so 
all may benefit.
date: Wed, 20 Feb 2008 10:55:01 -0500   author:   Jay Freedman

Re: VBA Word Count   
Hi GB,

select all of the doc.

MsgBox Selection.Words.Count
MsgBox Selection.Range.ComputeStatistics(wdStatisticWords)

The second comes closer to what you want,
though it is regrettable that Word uses different
methods for word count. 

>... macro that would count the number of words in bold.

>For example: 'MsgBox ActiveDocument.Range.Words.Count' returns an answer of 
>10 for a document that has 5 words in it. The word count on the menu 
>Tools...word count gives the correct answer. Is there a simple way to access 
>that, and why does words.count not just ummm count the words?

What a word constitutes,
is a fuzzy concept of fuzzy naturally language.
You would need a fuzzy computer, a contradiction in itself,
to count words, and it would return a fuzzy answer...

Try this, which is refinable at nauseam,
but will never be perfect.

Sub Test14()
Dim lBld As Long
Dim rDcm As Range
Dim oWrd As Range
Set rDcm = ActiveDocument.Range
For Each oWrd In rDcm.Words
   If oWrd.Font.Bold And Len(oWrd) > 1 Then
      lBld = lBld + 1
   End If
Next
MsgBox lBld
End Sub

Greetings from Bavaria, German
Helmut Weber MVP WordBVA
(MA linguistics)
date: Wed, 20 Feb 2008 17:26:03 +0100   author:   Helmut Weber

Re: VBA Word Count   
"Jay Freedman"  wrote in message 
news:uqDd0j9cIHA.5400@TK2MSFTNGP03.phx.gbl...

>> why does words.count not just count the words?

>
> The Words collection contains a separate item for each punctuation mark, 
> each paragraph mark, and each inline graphic in the document. That makes 
> it unsuitable for your purpose. The number in the Word Count dialog, which 
> matches the usual English definition of "words", is instead available from 
> the ComputeStatistics method.

Thanks very much, Jay. Your code (snipped) does the job nicely. That raises 
another question, namely where is there a fairly simple beginner's guide to 
the Word object model? The trouble with the help file is that it explains 
how to use a particular property or method, but it's not so good at helping 
to find the right one in the first place.
date: Wed, 20 Feb 2008 17:42:27 -0000   author:   GB

Re: VBA Word Count   
"Jay Freedman"  wrote in message 
news:uqDd0j9cIHA.5400@TK2MSFTNGP03.phx.gbl...
>    With myRange.Find
>        .Forward = True
>        .Wrap = wdFindStop
>        While .Execute
>            WordCount = WordCount + 1
>            myRange.Collapse wdCollapseEnd
>        Wend
>    End With

I was curious why the line 'myRange.Collapse wdCollapseEnd' needs to be 
there? I'm quite surprised that it works, as I would expect it to collapse 
the range you are finding in whilst still finding bold words in it. It seems 
to work quite nicely with or without that line in it.
date: Wed, 20 Feb 2008 17:55:28 -0000   author:   GB

Re: VBA Word Count   
GB wrote:
> "Jay Freedman"  wrote in message
> news:uqDd0j9cIHA.5400@TK2MSFTNGP03.phx.gbl...
>>    With myRange.Find
>>        .Forward = True
>>        .Wrap = wdFindStop
>>        While .Execute
>>            WordCount = WordCount + 1
>>            myRange.Collapse wdCollapseEnd
>>        Wend
>>    End With
>
> I was curious why the line 'myRange.Collapse wdCollapseEnd' needs to
> be there? I'm quite surprised that it works, as I would expect it to
> collapse the range you are finding in whilst still finding bold words
> in it. It seems to work quite nicely with or without that line in it.

The behavior of the Find object is something that defies understanding.

When the .Execute method succeeds (and returns True), the .Start and .End 
values of the Range object are changed to cover the found text. You can 
watch this happening in the Locals window as you single-step (F8) in the 
code. In some hidden bit of memory, though, VBA remembers the original range 
and continues to search through it. The exception to this occurs when the 
body of the While loop modifies the content of the Range object by adding or 
removing text, or by changing the .Start or .End value. That's when the 
.Collapse is required; if it's omitted, the next .Execute will indeed search 
within the _current_ location of the Range object.

Rather than forget the .Collapse when it is needed, and because it does no 
harm if it isn't needed, I have a standard "template" for a Find loop that 
includes it. Yes, you can take it out.

-- 
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so 
all may benefit.
date: Wed, 20 Feb 2008 13:54:44 -0500   author:   Jay Freedman

Re: VBA Word Count   
On Wed, 20 Feb 2008 17:42:27 -0000, "GB"  wrote:

>
>"Jay Freedman"  wrote in message 
>news:uqDd0j9cIHA.5400@TK2MSFTNGP03.phx.gbl...
>
>>> why does words.count not just count the words?
>
>>
>> The Words collection contains a separate item for each punctuation mark, 
>> each paragraph mark, and each inline graphic in the document. That makes 
>> it unsuitable for your purpose. The number in the Word Count dialog, which 
>> matches the usual English definition of "words", is instead available from 
>> the ComputeStatistics method.
>
>Thanks very much, Jay. Your code (snipped) does the job nicely. That raises 
>another question, namely where is there a fairly simple beginner's guide to 
>the Word object model? The trouble with the help file is that it explains 
>how to use a particular property or method, but it's not so good at helping 
>to find the right one in the first place.

I can't say I've kept up with what's in print on this subject. After all this
time, I just carry around large chunks of the object model in my head :-) and
use the help and the Object Browser (F2 in the VBA editor) to refresh my memory
when necessary.

There's a fairly good introduction to the concepts at
<http://msdn2.microsoft.com/en-us/library/aa272082(office.11).aspx>.

If you ask the help for the topic "Word Object Model (Table)" you'll get a
diagram of the top levels. Each box in the table is hyperlinked to the topic
about that object, and the topic has links at the top for Properties, Methods,
and (where applicable) Events.

If you're looking for a property or method and you don't know what it's called,
or what object it belongs to, use the Object Browser. Type into the search box
any term or fragment that you think might be related to what you want, and click
the Search (binoculars) button. You'll get a list of everything in the object
model whose names contain that fragment. 

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
date: Wed, 20 Feb 2008 19:56:30 -0500   author:   Jay Freedman

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us