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 / January 2005

Tip: Looking for answers? Try searching our database.

Help with Document Objects and other general questions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Maxey - 10 Jan 2005 03:53 GMT
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

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Greg Maxey - 10 Jan 2005 04:22 GMT
OK, I think I solved the word for word comparison issue.  I am now using
Find.Execute to sequence the counter.  I am still baffled about the
document.activate function.  I also think there should be a way to avoid
having to switch back and forth between the two documents.  Whatever that
way is, it is apparently over my head at present.

Sub WordListFrequency()

Dim ListArray
Dim i As Long
Dim j As Long
Dim k As Long
Dim myRange As Word.Range

'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
For i = LBound(ListArray) + 3 To UBound(ListArray) - 1 Step 3
 j = 0
 Set myRange = ActiveDocument.Range
 With myRange.Find
   .Text = ListArray(i)
   .MatchWildcards = False
   Do While .Execute
      Application.ScreenUpdating = False
      j = j + 1
   Loop
 End With
'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

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Hi,
>
[quoted text clipped - 55 lines]
>
> End Sub
 
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.