Macro range.word.count high

I am trying to write a macro to Return the Word count for each questions. I can make a selection but range.words.count returns a higher number than the Status bar word count (7 c.f. 5 or 256 Cf.  219) The specific Code I am using is

    'Go to Top of Documnet
    Selection.HomeKey Unit:=wdStory
    ' Skip Cover Sheet
    Application.Browser.Target = wdBrowsePage
    Application.Browser.Next
    'Store Current position
    StartLocation = Selection.Range.Start
    'Move To Next Heading
    Application.Browser.Target = wdBrowseHeading
    Application.Browser.Next
    EndLocation = Selection.Range.Start
   ' Select fromstart to end
    ActiveDocument.Range(Start:=StartLocation, End:=EndLocation).Select
    ' Get and Return the word count
    wc = Selection.Range.Words.Count
    MsgBox ("Words for this section are " & wc)

I can't see I have done something wrong inthe code; I can only see that the microsoft Selection object has the wrong value. Adding a watch to the selection also shows the High Value. Yet in the document at the same time the actual word count is the lower number ( and correct) 

To test : On the second page of the document place three paragraphs, Tow of style Heading 1 and the Middle paragraph a known number of words. 
Answer
Answer
Try Selection.Range.ComputeStatistics(wdStatisticWords) instead; it should return the correct value.
Stefan Blom
MS 365 Word MVP since 2005
Volunteer Moderator
MS 365, Win 11 Pro
~~~
Note that I do not work for Microsoft
https://mvp.microsoft.com/
~~~
Please specify Word version & OS in your question

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated April 14, 2025 Views 464 Applies to: