Consulting

Results 1 to 7 of 7

Thread: Count number of words each sentence

  1. #1

    Count number of words each sentence

    In a file I have multiple paragraphs. I would like to count the number of words in EACH sentence of a paragraph. How do I do that? Can anyone help to get me started?

    Paragraph1
    Sentence 1: 30 words
    Sentence 2: 15 words
    Sentence 3: 20 words

    Paragraph2
    Sentence 1: 22 words
    Sentence 2: 14 words
    Sentence 3: 20 words
    Sentence 4: 10 words

    Should not include space in the word count

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Word can't really do that intelligently. Although there is a VBA method for counting sentences, a sentence in VBA has nothing to do with grammar. For example, consider the following:

    Mr. Smith went to Dr. John's Grocery Store, to buy 1kg of Mrs. Green's Mt. Pleasant macadamia nuts.

    For you and me, that would probably count as one sentence; for VBA it counts as 5...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Hi Pau,

    Is there a way I could read each sentence (msgbox) and count the number of words (should not include space)?

  4. #4
    For example, the code below reads all sentences in a file. How do I add a line that includes the word count (does not include space)

    Public Sub ParseDoc()
    Dim paras As Paragraphs
    Dim para As Paragraph
    Dim sents As Sentences
    Dim sent As Range
    
    
    
    
        Set paras = ActiveDocument.Paragraphs
        For Each para In paras
            Set sents = para.Range.Sentences
            For Each sent In sents
                MsgBox (sent.Text)
            Next
        Next
    End Sub

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You don't seem to be paying attention. Please read my previous post.

    As described in my previous post, for VBA, 'Mr.' is a sentence. So is 'Smith went to Dr.' and 'John's Grocery Store, to buy 1kg of Mrs.' and '' and 'Green's Mt.' and 'Pleasant macadamia nuts.'. I doubt that's much use to anyone.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    so you're saying we couldn't get an accurate count based on the example you posted above?

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by swaggerbox View Post
    so you're saying we couldn't get an accurate count based on the example you posted above?
    Well, if you think 5 as the sentence count for the example given is accurate, the answer is yes; otherwise, it's no.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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