Reply
 
Thread Tools Display Modes
  #1   Report Post  
Henley writer
 
Posts: n/a
Default How can I wordcount multiple files without opening them?

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!
  #2   Report Post  
Greg Maxey
 
Posts: n/a
Default

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.



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

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!



  #4   Report Post  
Greg Maxey
 
Posts: n/a
Default

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!



  #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!
Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I open multiple Word 2003 files to display on dual monitors mally Microsoft Word Help 6 July 4th 05 11:00 AM
Working on multiple word files from a single file???? Rich New Users 5 May 30th 05 03:10 PM
Office 2003 opening shared files from a computer with office 97. Robert Microsoft Word Help 1 January 25th 05 03:48 PM
Word opening files Read Only when not Nate Weldon Microsoft Word Help 1 January 21st 05 02:37 AM
How can I insert multiple files at once into a document? Confused Word User Microsoft Word Help 1 December 10th 04 05:12 PM


All times are GMT +1. The time now is 08:44 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"