G
Greg Maxey
Hi,
I am trying to create a macro that will count the occurrence of a list of
Words. The list of words is in a two column document with the words listed
in column 1. The macro compares each word in column 1 with each word in the
document and records the number of matches in column 2.
What I have posted below works (seems to), but I am not clear on the use of
document objects. Basically I have the document open and then open the word
list to create the array. Once the array is created I switch back to the
document (why do I have to use documents(2).activate). It seems I should be
using documents(1) since it was the orginal open document and appears in the
Window menu as #1. Please explain this.
I sense that what I have created in grossly inefficient and want to believe
that there is a better way than doing a word by word comparison for each
word in the list. I toyed with the idea of finding and replacing the words
with themselves but couldn't figure out out to get a count. Ideas?
Thanks.
Sub WordFrequency()
Dim ListArray
Dim i As Long
Dim j As Long
Dim k As Long
'Open and build and array of words to count
Set WordList = Documents.Open(FileName:="D:\My Documents\Word\Word
Documents\Word Tips\Macros\Word List.doc")
ListArray = WordList.Tables(1).Range.Text
ListArray = Split(ListArray, Chr(13) & Chr(7))
'Active the orginal document (don't know why this is 2 rather that 1)
Documents(2).Activate
'need this starting k value later on
k = 2
'compare each word in the list with the words in the doc
'count if match occurs
For i = LBound(ListArray) + 3 To UBound(ListArray) - 1 Step 3
j = 0
Selection.HomeKey Unit:=wdStory
System.Cursor = wdCursorWait
For Each aword In ActiveDocument.Words
If Trim(LCase(aword)) Like ListArray(i) Then
j = j + 1
End If
Next
'switch to the word list
Documents(1).Activate
'record the count
ActiveDocument.Tables(1).Cell(k, 2).Range.Text = j
k = k + 1
'switch back to the orginal document
Documents(2).Activate
Next
End Sub
I am trying to create a macro that will count the occurrence of a list of
Words. The list of words is in a two column document with the words listed
in column 1. The macro compares each word in column 1 with each word in the
document and records the number of matches in column 2.
What I have posted below works (seems to), but I am not clear on the use of
document objects. Basically I have the document open and then open the word
list to create the array. Once the array is created I switch back to the
document (why do I have to use documents(2).activate). It seems I should be
using documents(1) since it was the orginal open document and appears in the
Window menu as #1. Please explain this.
I sense that what I have created in grossly inefficient and want to believe
that there is a better way than doing a word by word comparison for each
word in the list. I toyed with the idea of finding and replacing the words
with themselves but couldn't figure out out to get a count. Ideas?
Thanks.
Sub WordFrequency()
Dim ListArray
Dim i As Long
Dim j As Long
Dim k As Long
'Open and build and array of words to count
Set WordList = Documents.Open(FileName:="D:\My Documents\Word\Word
Documents\Word Tips\Macros\Word List.doc")
ListArray = WordList.Tables(1).Range.Text
ListArray = Split(ListArray, Chr(13) & Chr(7))
'Active the orginal document (don't know why this is 2 rather that 1)
Documents(2).Activate
'need this starting k value later on
k = 2
'compare each word in the list with the words in the doc
'count if match occurs
For i = LBound(ListArray) + 3 To UBound(ListArray) - 1 Step 3
j = 0
Selection.HomeKey Unit:=wdStory
System.Cursor = wdCursorWait
For Each aword In ActiveDocument.Words
If Trim(LCase(aword)) Like ListArray(i) Then
j = j + 1
End If
Next
'switch to the word list
Documents(1).Activate
'record the count
ActiveDocument.Tables(1).Cell(k, 2).Range.Text = j
k = k + 1
'switch back to the orginal document
Documents(2).Activate
Next
End Sub