Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

Need Word2000 code to count how many time every word are repeat in text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dado - 15 Aug 2006 09:34 GMT
Hi.

I'm not programer.
I'm student and can't pay for this.

I have Word2000.

I need to count, for every word in text, how many time that word repeat.
And that I can sort result in few ways (max, min, a-z, z-a, etc).

For example this text:
"text" - 4 times
"Word2000" - 2 times
_same for all words in text_
etc.

If anyone can programme something like this?

Thanks.
Jezebel - 15 Aug 2006 11:36 GMT
You don't need any programming.

1. Make a copy of your document.
2. Use Find and Replace to remove all punctuation from the document, and to
replace all spaces with paragraph marks. At which point your document should
be just a list of words.
3. Copy this list and paste into Excel.
4. Sort the list, then use count formulas in the adjacent cells.

> Hi.
>
[quoted text clipped - 15 lines]
>
> Thanks.
dado - 15 Aug 2006 16:34 GMT
> You don't need any programming.
>
[quoted text clipped - 3 lines]
> should be just a list of words.
> 3. Copy this list and paste into Excel.

I did all this, and it works.
But it is a problem if there is more hundreds pages doc file.

> 4. Sort the list, then use count formulas in the adjacent cells.

Can you help me here, I'm not good in Excel?
What formulas to use to get list of all words and how many time every word
repeat.

Thanks.

P.S. There is no any software who can do that (with or without Word)?
Doug Robbins - Word MVP - 15 Aug 2006 17:50 GMT
Use

Sub WordFrequency()

        Dim SingleWord As String           'Raw word pulled from doc
        Const maxwords = 9000              'Maximum unique words allowed
        Dim Words(maxwords) As String      'Array to hold unique words
        Dim Freq(maxwords) As Integer      'Frequency counter for Unique
Words
        Dim WordNum As Integer             'Number of unique words
        Dim ByFreq As Boolean              'Flag for sorting order
        Dim ttlwds As Long                 'Total words in the document
        Dim Excludes As String             'Words to be excluded
        Dim Found As Boolean               'Temporary flag
        Dim j, k, l, Temp As Integer       'Temporary variables
        Dim tword As String                '

        ' Set up excluded words
'         Excludes =
"[the][a][of][is][to][for][this][that][by][be][and][are]"
        Excludes = ""
        Excludes = InputBox$("Enter words that you wish to exclude,
surrounding each word with [ ].", "Excluded Words", "")
'        Excludes = Excludes & InputBox$("The following words are excluded:
" & Excludes & ". Enter words that you wish to exclude, surrounding each
word with [ ].", "Excluded Words", "")
        ' Find out how to sort
        ByFreq = True
        Ans = InputBox$("Sort by WORD or by FREQ?", "Sort order", "FREQ")
        If Ans = "" Then End
        If UCase(Ans) = "WORD" Then
            ByFreq = False
        End If

        Selection.HomeKey Unit:=wdStory
        System.Cursor = wdCursorWait
        WordNum = 0
        ttlwds = ActiveDocument.Words.Count
        Totalwords = ActiveDocument.Words.Count

        ' Control the repeat
        For Each aword In ActiveDocument.Words
            SingleWord = Trim(aword)
            If SingleWord < "A" Or SingleWord > "z" Then SingleWord = ""
'Out of range?
            If InStr(Excludes, "[" & SingleWord & "]") Then SingleWord = ""
'On exclude list?
            If Len(SingleWord) > 0 Then
                Found = False
                For j = 1 To WordNum
                    If Words(j) = SingleWord Then
                        Freq(j) = Freq(j) + 1
                        Found = True
                        Exit For
                    End If
                Next j
                If Not Found Then
                    WordNum = WordNum + 1
                    Words(WordNum) = SingleWord
                    Freq(WordNum) = 1
                End If
                If WordNum > maxwords - 1 Then
                    j = MsgBox("The maximum array size has been exceeded.
Increase maxwords.", vbOKOnly)
                    Exit For
                End If
            End If
            ttlwds = ttlwds - 1
            StatusBar = "Remaining: " & ttlwds & "     Unique: " & WordNum
        Next aword

        ' Now sort it into word order
        For j = 1 To WordNum - 1
            k = j
            For l = j + 1 To WordNum
                If (Not ByFreq And Words(l) < Words(k)) Or (ByFreq And
Freq(l) > Freq(k)) Then k = l
            Next l
            If k <> j Then
                tword = Words(j)
                Words(j) = Words(k)
                Words(k) = tword
                Temp = Freq(j)
                Freq(j) = Freq(k)
                Freq(k) = Temp
            End If
            StatusBar = "Sorting: " & WordNum - j
        Next j

        ' Now write out the results
        tmpName = ActiveDocument.AttachedTemplate.FullName
        Documents.Add Template:=tmpName, NewTemplate:=False
        Selection.ParagraphFormat.TabStops.ClearAll
        With Selection
            For j = 1 To WordNum
                .TypeText Text:=Words(j) & vbTab & Trim(Str(Freq(j))) &
vbCrLf
            Next j
        End With
        ActiveDocument.Range.Select
        Selection.ConvertToTable
        Selection.Collapse wdCollapseStart
        ActiveDocument.Tables(1).Rows.Add BeforeRow:=Selection.Rows(1)
        ActiveDocument.Tables(1).Cell(1, 1).Range.InsertBefore "Word"
        ActiveDocument.Tables(1).Cell(1, 2).Range.InsertBefore
"Occurrences"
        ActiveDocument.Tables(1).Range.ParagraphFormat.Alignment =
wdAlignParagraphCenter
        ActiveDocument.Tables(1).Rows.Add
        ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
1).Range.InsertBefore "Total words in Document"
        ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
2).Range.InsertBefore Totalwords
        ActiveDocument.Tables(1).Rows.Add
        ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
1).Range.InsertBefore "Number of different words in Document"
        ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
2).Range.InsertBefore Trim(Str(WordNum))
        System.Cursor = wdCursorNormal
     '   j = MsgBox("There were " & Trim(Str(WordNum)) & " different words
", vbOKOnly, "Finished")
    Selection.HomeKey wdStory

End Sub

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

>> You don't need any programming.
>>
[quoted text clipped - 16 lines]
>
> P.S. There is no any software who can do that (with or without Word)?
dado - 15 Aug 2006 18:32 GMT
> Hope this helps.
>
> Please reply to the newsgroup unless you wish to avail yourself of my
> services on a paid consulting basis.
>
> Doug Robbins - Word MVP

Thanks Doug, it works great!

I did try but I don't know how to add frist column with numbers (1, 2, 3, 4,
5, etc.)?
Can you also help me with this?

Thanks Doug again!
Doug Robbins - Word MVP - 15 Aug 2006 20:50 GMT
Sorry, I do not understand what you mean by add first column with numbers
(1, 2, 3, 4, 5,)

Maybe all you need to do is insert a column to the left and then select that
column and click on the numbering button on the toolbar.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

>> Hope this helps.
>>
[quoted text clipped - 10 lines]
>
> Thanks Doug again!
dado - 16 Aug 2006 12:05 GMT
> Sorry, I do not understand what you mean by add first column with numbers
> (1, 2, 3, 4, 5,)

When your Word Macro finish, to have tree columns, like this:

No.  -  Word  -  Occurrences
1           and          95
2            to            90
3            you         85
etc.

I did try to add first column, but I get error.
Doug Robbins - Word MVP - 16 Aug 2006 20:41 GMT
Just add the column after the macro finishes and then populate it with the
numbers by clicking on the numbering button.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

>> Sorry, I do not understand what you mean by add first column with numbers
>> (1, 2, 3, 4, 5,)
[quoted text clipped - 8 lines]
>
> I did try to add first column, but I get error.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.