Results 1 to 10 of 10
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What's with the Range.Words.Count? (Word97/SR2)

    What's with the Range.Words.Count?

    I write essays. I like to know how many words are in the essay (editors are curmudgeous). Real words, not including the short biography and the page header ("Please send money to ....").

    Using the properties box, indirectly, as { NumWords} doesn't help, because it includes the biography. I tried { {NumWords} - 48 }, but that failed when my biography changed from being 48 words long.

    Then I hit on the idea of using a short macro to report the number of words in a SELECTION, figuring that if I were over the 300 word limit, I could more easily test paragraphs as candidates for exclusion:

    <pre>Sub WordCount()
    MsgBox Selection.Range.Words.Count
    End Sub
    </pre>


    On the Toolbar it's just dandy, but it consistently reports many more words than does { NumWords}.

    So I found that a paragraph mark appears to be a word, and this works much better:
    <pre>Sub WordCount()
    MsgBox Selection.Range.Words.Count - Selection.Paragraphs.Count
    End Sub
    </pre>


    However, the second method works well only if complete paragraphs are selected.

    Without getting into jiggery-pokey ("add one to the count if the trailing character is NOT a paragraph mark unless Venus is in the ascendant ..."), is there a better way of quickly reporting the number of words, real words, strings of characters, in a range?

  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: What's with the Range.Words.Count? (Word97/SR2)

    This is a simple function I wrote to count the number of words in a text string, "word" defined as any one or more continguous characters other than a space, separated by one or more contiguous spaces. I did not concern myself with hyphens, etc - tried to keep it simple. (Function is actually counting contiguous spaces, not words....) Don't know how well will work with Word - tested with Word docs saved as text files, and the word count reported by Word (in "Document Statistics") varied widely with word count reported by function. I don't know what Word counts - paragraph marks, who knows?? --I am no Word VBA expert - mainly use this function with Access database to count words in Memo fields; testing with table with appx 10,000 memo fields with lots of "verbiage", function's perfomance was reasonably quick. Here is code if interested:

    Public Function GetWordCount(ByVal strText As String) As Long
    On Error Resume Next

    Dim lngLen As Long
    Dim lngCount As Long
    Dim n As Long
    Const CON_SPACE As String = " "

    strText = Trim$(strText)
    lngLen = Len(strText)

    If lngLen = 0 Then
    GetWordCount = 0
    Exit Function
    End If

    For n = 1 To lngLen
    n = InStr(n, strText, CON_SPACE, vbBinaryCompare)
    If n = 0 Then ' Bail out
    Exit For
    Else
    ' Test preceding char - do not count contiguous spaces more than once
    If Mid(strText, n - 1, 1) <> CON_SPACE Then
    lngCount = lngCount + 1
    End If
    End If
    Next
    GetWordCount = lngCount + 1

    End Function

    HTH

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: What's with the Range.Words.Count? (Word97/SR2)

    Does Word 97 have the ComputeStatistics method?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: What's with the Range.Words.Count? (Word97/SR2)

    Yes, ComputeStatistics is there in Word 97, as a method of the Document and Range objects.

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: What's with the Range.Words.Count? (Word97/SR2

    Mark , thanks for this.

    I'm curious. Why the On Error at the head?

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: What's with the Range.Words.Count? (Word97/SR2

    Hans, Jefferson, thanks for these responses.

    The ComputeStatistics method seems to return a value equal to that reported in the File, Properties box.

    I suspect, therefore, that ComputeStatistics is counting Paragraph marks.

  7. #7
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: What's with the Range.Words.Count? (Word97/SR2

    The main reason for the On Error Resume Next in this case was, I was using function in an Access query using field in table as the argument for function. Meaning the function will execute once for each row (record) returned by the query. Meaning if you used a generic error-handler (msgbox displaying error number, etc) and there was a bug in function, and query returns 10,000 records, you get to click "OK" 10,000 times....

  8. #8
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: What's with the Range.Words.Count? (Word97/SR2

    Does this Woody's Watch Archive help, kinda, sorta? Look about 7/8ths of the way down the issue.
    -John ... I float in liquid gardens
    UTC -7ąDS

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: What's with the Range.Words.Count? (Word97/SR2

    > , you get to click "OK" 10,000 times....

    ...but this hasn't happened to you yet, right? (grin!).


    Thanks for the comment. I was a tad puzzled .

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: What's with the Range.Words.Count? (Word97/SR2

    > Look about 7/8ths of the way down the issue

    0.875 Be damned! (grin) I read the entire issue. I think I once received that issue, it was about the time I "started" with TUG. Good reading.


    I'm going to collect all these small essays in "words" statistics and post them as a summary. First I have to recall my original intent .....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •