Home |
Search |
Today's Posts |
|
#1
|
|||
|
|||
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
|
|||
|
|||
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! |
#3
|
|||
|
|||
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! |
#4
|
|||
|
|||
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:
|
#5
|
|||
|
|||
See http://word.mvps.org/faqs/macrosvba/DSOFile.htm
-- Suzanne S. Barnhill Microsoft MVP (Word) Words into Type Fairhope, Alabama USA Word MVP FAQ site: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit. "Henley writer" Henley wrote in message ... 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 | |
|
|
Similar Threads | ||||
Thread | Forum | |||
How do I open multiple Word 2003 files to display on dual monitors | Microsoft Word Help | |||
Working on multiple word files from a single file???? | New Users | |||
Office 2003 opening shared files from a computer with office 97. | Microsoft Word Help | |||
Word opening files Read Only when not | Microsoft Word Help | |||
How can I insert multiple files at once into a document? | Microsoft Word Help |