View Single Post
  #5   Report Post  
gary5 gary5 is offline
Junior Member
 
Posts: 0
Default

Thank You Greg Maxey for the script.

I am new to this forum,
I used the same script but I am not getting the correct word count?
I guess it is also counting spaces or tabs.
Just want one confirmation is the line " pWordCount = .Words.Count " counts the spaces,header,footer as well ?

Also please let me know
1. If we can get the number of repeated words in the document?
2. How can we get only the word count (except spaces, header, footer)


Regards,
Gary



==================
Quote:
Originally Posted by Greg Maxey View Post
See the method Suzanne directed you to. If is far superior.



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Greg Maxey wrote:
AFAIK they have to be opened, but it can be automated. The macro
below is relatively crude in the way the data is displayed, but it
will work.
Put all of the files in a dedicated folder. I used C:\Batch Files.

Create a new blank document with a single row two column table. Label the
left column Document Name and the right column Word count.

Put this macro in the document and run it:

Sub BacthFileCountWords()
'Compiled by Greg Maxey

Dim MyFile As String
Dim PathToUse As String
Dim pDocName As String
Dim pWordCount As Long
Dim Counter As Long
Dim myDoc As Document
Dim i As Long

'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000) '1000 is arbitrary

'Specify folder containing files
PathToUse = "C:\Batch Folder\"
'Loop through all the files of *.doc type in the directory by using
Dir$ function
MyFile = Dir$(PathToUse & "*.doc")
'For each file found add to the array
Do While MyFile ""
DirectoryListArray(Counter) = MyFile
'Get the next file name
MyFile = Dir$
Counter = Counter + 1
Loop

'Reset the size of the array without losing its values by using Redim
Preserve
ReDim Preserve DirectoryListArray(Counter - 1)
Application.ScreenUpdating = False
i = 2
For Counter = 0 To UBound(DirectoryListArray)
Set myDoc = Documents.Open(FileName:=PathToUse &
DirectoryListArray(Counter), _
Visible:=False)
With myDoc
pDocName = .Name
pWordCount = .Words.Count
.Save
.Close
End With
ActiveDocument.Tables(1).Cell(i, 1).Range.Text = pDocName
ActiveDocument.Tables(1).Cell(i, 2).Range.Text = pWordCount
i = i + 1
ActiveDocument.Tables(1).Rows.Add
Next Counter
ActiveDocument.Tables(1).Rows.Last.Delete
Application.ScreenUpdating = True
End Sub


Masters, if you can offer a more graceful way of displaying the data
please advise.




Henley writer wrote:
I am expecting to receive a batch of about 700 files and will need to
do a word count on each of them. How can I do this automatically? I
don't want to have to open each one, do a wordcount and then write
down the answer! Is it possible to generate a list of their
individual word counts without opening them? I can make sure they are
all in one folder if necessary.

Thanks for your help!