Results 1 to 7 of 7

Thread: Creating cummalative word counter in word.

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    15

    Arrow Creating cummalative word counter in word.

    Hi all
    need a little help
    im tying to create a macro in word that will count each word length and store them in a varible.
    For example if it counts the word two it would add one to the under5 varible e.g
    VB Code:
    1. If word.length <= 5 then"how ever this is done as this is my problem
    2. under5 = under5 + 1
    3. End If
    so in short i need it to create around 5 varibles under5, under10, under15, under20 and under25
    go through each word in the document (ebook) and then output the result some how however is easiest but so i can see it.

    This is for my GCSE mathmatics invistigation just to give you an idea of why on earth I would want such a thing it is a statistics peice comparing adult books to childrens or in my case ebooks.

    Thanks in advance Paul (Shorty)
    If you = Good
    then goto heaven
    else goto hell
    End If

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    VB Code:
    1. Sub WordStats()
    2.     Dim myText() As String, nWord As String
    3.     Dim to5, to10, to15, to20, to25
    4.  
    5.     ' Reset variables
    6.     to5 = 0
    7.     to10 = 0
    8.     to15 = 0
    9.     to20 = 0
    10.     to25 = 0
    11.  
    12.     Selection.WholeStory
    13.    
    14.     ' Split the document into words
    15.     myText = Split(Selection.Text, " ")
    16.    
    17.     ' Loop through each word
    18.     For Each nWord In myText
    19.         ' Remove any punctuation to prevent it from being counted as part of the word
    20.         nWord = Replace(nWord, "?", "")
    21.         nWord = Replace(nWord, ".", "")
    22.         nWord = Replace(nWord, ",", "")
    23.         nWord = Replace(nWord, "!", "")
    24.         nWord = Replace(nWord, "'", "")
    25.         nWord = Replace(nWord, Chr(34), "")
    26.        
    27.         ' Check which case it falls into
    28.         Select Case Len(nWord)
    29.         Case Is <= 5
    30.             Debug.Print nWord & " (5)"
    31.             to5 = to5 + 1
    32.         Case Is <= 10
    33.             Debug.Print nWord & " (10)"
    34.             to10 = to10 + 1
    35.         Case Is <= 15
    36.             Debug.Print nWord & " (15)"
    37.             to15 = to15 + 1
    38.         Case Is <= 20
    39.             Debug.Print nWord & " (20)"
    40.             to20 = to20 + 1
    41.         Case Is > 20
    42.             Debug.Print nWord & " (25)"
    43.             to25 = to25 + 1
    44.         End Select
    45.     Next
    46.    
    47.     ' Display the results
    48.     MsgBox "Words" & vbCrLf & " < 5 = " & to5 & vbCrLf & " < 10 = " & to10 & vbCrLf & " < 15 = " & to15 & vbCrLf & " < 20 = " & to20 & vbCrLf & " > 20 = " & to25
    49. End Sub

    Hope that helps you

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    15
    Thanks for your help but when running the macro I get this error.

    VB Code:
    1. For Each nWord In myText 'here the debugger comes up with this error msg For Each control variable on arrays must be Variant
    i have had a go at debugging it my self but with not much look as im not very expericed in VBA but more just with VB
    Again thanks in advance Shorty
    If you = Good
    then goto heaven
    else goto hell
    End If

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    That will be because nWord has been declared as a string variable data type & not a variant, also, all of the counter variables ARE variants. Try this one instead:
    VB Code:
    1. [color="#0000A0"]Private[/color] [color="#0000A0"]Sub[/color] CommandButton1_Click()
    2.     [color="#0000A0"]Dim[/color] straryFullDocText() [color="#0000A0"]As[/color] [color="#0000A0"]String[/color]
    3.     [color="#0000A0"]Dim[/color] intElementCounter [color="#0000A0"]As[/color] [color="#0000A0"]Integer[/color]
    4.  
    5.     [color="#0000A0"]Dim[/color] intUnder5Chrs [color="#0000A0"]As[/color] [color="#0000A0"]Integer[/color]
    6.     [color="#0000A0"]Dim[/color] intUnder10Chrs [color="#0000A0"]As[/color] [color="#0000A0"]Integer[/color]
    7.     [color="#0000A0"]Dim[/color] intUnder15Chrs [color="#0000A0"]As[/color] [color="#0000A0"]Integer[/color]
    8.     [color="#0000A0"]Dim[/color] intUnder20Chrs [color="#0000A0"]As[/color] [color="#0000A0"]Integer[/color]
    9.     [color="#0000A0"]Dim[/color] intUnder25Chrs [color="#0000A0"]As[/color] [color="#0000A0"]Integer[/color]
    10.    
    11.     straryFullDocText = Split(ActiveDocument.Content.Text, " ")
    12.    
    13.     [color="#0000A0"]For[/color] intElementCounter = 0 [color="#0000A0"]To[/color] UBound(straryFullDocText)
    14.         Replace straryFullDocText(intElementCounter), "?", ""
    15.         Replace straryFullDocText(intElementCounter), ".", ""
    16.         Replace straryFullDocText(intElementCounter), ",", ""
    17.         Replace straryFullDocText(intElementCounter), "!", ""
    18.         Replace straryFullDocText(intElementCounter), "'", ""
    19.         Replace straryFullDocText(intElementCounter), Chr(34), ""
    20.  
    21.         Select Case Len(straryFullDocText(intElementCounter))
    22.         Case Is <= 5
    23.             intUnder5Chrs = intUnder5Chrs + 1
    24.         Case Is <= 10
    25.             intUnder10Chrs = intUnder10Chrs + 1
    26.         Case Is <= 15
    27.             intUnder15Chrs = intUnder15Chrs + 1
    28.         Case Is <= 20
    29.             intUnder20Chrs = intUnder20Chrs + 1
    30.         Case Is <= 25
    31.             intUnder25Chrs = intUnder25Chrs + 1
    32.         [color="#0000A0"]End[/color] Select
    33.     [color="#0000A0"]Next[/color] intElementCounter
    34.    
    35.     MsgBox "Count of words under 5 characters: " & intUnder5Chrs & vbCrLf & _
    36.     "Count of words under 10 characters: " & intUnder10Chrs & vbCrLf & _
    37.     "Count of words under 15 characters: " & intUnder15Chrs & vbCrLf & _
    38.     "Count of words under 20 characters: " & intUnder20Chrs & vbCrLf & _
    39.     "Count of words under 25 characters: " & intUnder25Chrs
    40. [color="#0000A0"]End[/color] [color="#0000A0"]Sub[/color]

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    15
    Again thanks for everybody's help but there is still a problem wich im sure you great lot can solve for me as im so stupid.

    When I excute the code it works fine for smaller texts but when I try to count up my ebook I come accross and error that error is Run-time error 6 overflow.
    VB Code:
    1. For intElementCounter = 0 To UBound(straryFullDocText) " This is what the debugger highlights
    Thanks again for your help and this seams to be coming along beutifully
    Oh and by the way my I.T teacher at school coulnt even work this one out.

    Below I have attached Half of Alice in wonderland with the macro embeded just open and run the macro and you will see the problem
    Thanks in advance Shorty
    Attached Files Attached Files
    If you = Good
    then goto heaven
    else goto hell
    End If

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Oh and by the way my I.T teacher at school coulnt even work this one out.
    If he's a vb programmer then backhand him, if he's a normal IT guy then fair enough, basically the integer data variable type can hold a number from -32,768 to 32,767, if your number you're trying to hold in one of the variables is above this, then you get this error.

    Try changing all of the variables from an integer data type to a long data type which can support a greater numeric range from -2,147,483,648 to 2,147,483,647...

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    15
    Ah of course how stupid of me of course even I should of known that one anywayz thanks again and now it works like a gem cheers Paul
    If you = Good
    then goto heaven
    else goto hell
    End If

Posting Permissions

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



Click Here to Expand Forum to Full Width