How do I check word count of multiple word documents?

S

Sujal

I have over 7500 documents and need to check word count of all of them and
store them in a database. Additionally I need to append the word count into
the 7500+ word docs. How can I do this programmatically? HELP!
 
C

Cooz

Hi Sujal,

You can do this by means of the following macro. Be aware that if you run
the macro to check 7500+ documents, you cannot use the machine on which the
macro runs for a Long Time. It is probably the best to dedicate a machine to
this task.

Make sure you have all documents in (subdirectories in) one directory.
Make a new blank document and save it.
Record any macro, say record clicking the B-button on the Standard toolbar,
and be sure to choose the document under 'Store macro in'.
Choose Tools | Macro > Macro's..., select your macro and choose Edit. The
VBA-editor opens.
Replace the entire macro by the following:
---
Sub CountWords()
Const strTableDoc As String = "G:\temp\TableDoc.doc"
Dim intN As Integer, lngWordCount As Long, intSlashPos As Integer

' initialize
Application.ScreenUpdating = False
WordBasic.DisableAutoMacros 1 ' no automacros please

' new doc to store the database table
Documents.Add
ActiveDocument.SaveAs FileName:=strTableDoc
Selection.TypeText "Filename" & vbTab & "WordCount" & vbCrLf

' find all files
With Application.FileSearch
.FileName = "*.doc"
.LookIn = "G:\temp\temp"
.SearchSubFolders = True
.Execute

' loop through all files
For intN = 1 To .FoundFiles.Count

' insert word count in file
Documents.Open FileName:=.FoundFiles(intN),
AddToRecentFiles:=False
lngWordCount = ActiveDocument.Words.Count
Selection.EndKey Unit:=wdStory
Selection.TypeText vbCrLf & vbCrLf & LTrim(Str(lngWordCount))
ActiveDocument.Save
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

' update table
Documents(strTableDoc).Activate
intSlashPos = InStrRev(.FoundFiles(intN), "\")
Selection.TypeText Text:=Mid(.FoundFiles(intN), intSlashPos + 1)
& _
vbTab & LTrim(Str(lngWordCount)) & vbCrLf

Next intN

End With

' make table and save
With Documents(strTableDoc)
.Range.ConvertToTable Separator:=wdSeparateByTabs, NumColumns:=2
.Save
End With

' finished
WordBasic.DisableAutoMacros 0 ' reset
Application.ScreenUpdating = True
MsgBox "Finished!", vbOKOnly, "CountWords"

End Sub
---
Click the Save button in the VBA editor to save your document. Close the
VBA-editor.
Run the macro (Tools | Macro > Macros...) and go spend your time somewhere
else.

Some brief notes:
Replace "G:\temp\temp" by the directory in which your documents are stored.
The macro stores the word count on the last paragraph of every document. When
the macro is finished, it displays a message that says so. The macro produces
a document with a Word table that you can (convert and) import into your
database. You cannot use the macro if you have documents that retrieve data
from somewhere else, e.g. via a mailmerge. Perhaps you should test it on a
few documents first so you can see if this is what you want.

Good luck,
Cooz
 
S

Sujal

Thanks very much!

Cooz said:
Hi Sujal,

You can do this by means of the following macro. Be aware that if you run
the macro to check 7500+ documents, you cannot use the machine on which the
macro runs for a Long Time. It is probably the best to dedicate a machine to
this task.

Make sure you have all documents in (subdirectories in) one directory.
Make a new blank document and save it.
Record any macro, say record clicking the B-button on the Standard toolbar,
and be sure to choose the document under 'Store macro in'.
Choose Tools | Macro > Macro's..., select your macro and choose Edit. The
VBA-editor opens.
Replace the entire macro by the following:
---
Sub CountWords()
Const strTableDoc As String = "G:\temp\TableDoc.doc"
Dim intN As Integer, lngWordCount As Long, intSlashPos As Integer

' initialize
Application.ScreenUpdating = False
WordBasic.DisableAutoMacros 1 ' no automacros please

' new doc to store the database table
Documents.Add
ActiveDocument.SaveAs FileName:=strTableDoc
Selection.TypeText "Filename" & vbTab & "WordCount" & vbCrLf

' find all files
With Application.FileSearch
.FileName = "*.doc"
.LookIn = "G:\temp\temp"
.SearchSubFolders = True
.Execute

' loop through all files
For intN = 1 To .FoundFiles.Count

' insert word count in file
Documents.Open FileName:=.FoundFiles(intN),
AddToRecentFiles:=False
lngWordCount = ActiveDocument.Words.Count
Selection.EndKey Unit:=wdStory
Selection.TypeText vbCrLf & vbCrLf & LTrim(Str(lngWordCount))
ActiveDocument.Save
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

' update table
Documents(strTableDoc).Activate
intSlashPos = InStrRev(.FoundFiles(intN), "\")
Selection.TypeText Text:=Mid(.FoundFiles(intN), intSlashPos + 1)
& _
vbTab & LTrim(Str(lngWordCount)) & vbCrLf

Next intN

End With

' make table and save
With Documents(strTableDoc)
.Range.ConvertToTable Separator:=wdSeparateByTabs, NumColumns:=2
.Save
End With

' finished
WordBasic.DisableAutoMacros 0 ' reset
Application.ScreenUpdating = True
MsgBox "Finished!", vbOKOnly, "CountWords"

End Sub
---
Click the Save button in the VBA editor to save your document. Close the
VBA-editor.
Run the macro (Tools | Macro > Macros...) and go spend your time somewhere
else.

Some brief notes:
Replace "G:\temp\temp" by the directory in which your documents are stored.
The macro stores the word count on the last paragraph of every document. When
the macro is finished, it displays a message that says so. The macro produces
a document with a Word table that you can (convert and) import into your
database. You cannot use the macro if you have documents that retrieve data
from somewhere else, e.g. via a mailmerge. Perhaps you should test it on a
few documents first so you can see if this is what you want.

Good luck,
Cooz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top