Word/Character Count for a Group of Documents

B

Brian Smith

Does anyone know where I could find some VBA code that would help me extract
the word and character counts for a group of Word documents in a given
folder? Ideally, I would like to extract this information and have it go
directly into a new document (in a table preferably). The table would look
something like the following.

File Name Word Count Character Count
xx1.doc 5,204 25,000
xx2.doc 7,500 39,500
etc.

Thanks in advance for any help/pointers. I'm assuming others have had a
similar need in the past and someone has already solved this problem. By the
way, I'm using Word 2003 under XP both with SP2.

Brian
 
S

StevenM

To: Brian Smith,

'
' Macro: CountWordsCharactersInFolder
' This macro opens all the "Doc" files
' in a folder and places the file's name,
' word count, and character count in a table
' in the active document.
'
Sub CountWordsCharactersInFolder()
Dim sPath As String
Dim sActivePath As String
Dim sFileName As String
Dim newDoc As Document
Dim oRange As Range
Dim oTable As Table
Dim oRow As Row
'
' Make sure we're not puting a table inside another table
'
Set oRange = Selection.Range
oRange.Collapse wdCollapseStart
oRange.Select
If Selection.Information(wdWithInTable) = True Then
MsgBox "Move Cursor out of the table."
Exit Sub
End If
'
' Hard code path here, or let sPath = "" to get path from user.
'
sPath = ""
If sPath = "" Then
With Dialogs(wdDialogCopyFile)
If .Display() <> -1 Then Exit Sub
sPath = .Directory
End With
End If

If Len(sPath) = 0 Then Exit Sub
If Asc(sPath) = 34 Then
sPath = Mid(sPath, 2, Len(sPath) - 2)
End If
'
' Make sure that the active document is not in the folder
' (It causes problems when one attempts to close it.)
'
sActivePath = ActiveDocument.Path & Application.PathSeparator
If sPath = sActivePath Then
MsgBox "This Document is in the selected folder."
Exit Sub
End If
'
' Create table
'
Set oTable = oRange.Tables.Add(Range:=oRange, NumRows:=1, NumColumns:=3)
Set oRow = oTable.Rows(1)
With oRow
.Cells(1).Range.Text = "File Names"
.Cells(2).Range.Text = "Word Counts"
.Cells(2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Cells(3).Range.Text = "Character Counts"
.Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
End With
'
' Find Files
'
sFileName = Dir(sPath & "*.doc")
While sFileName > ""
Set newDoc = Documents.Open(sFileName, Visible:=False)
Set oRows = oTable.Rows.Add
With oRows
.Cells(1).Range.Text = sFileName
.Cells(2).Range.Text = newDoc.Words.Count
.Cells(3).Range.Text = newDoc.Characters.Count
End With
newDoc.Close
sFileName = Dir
Wend
'
' Done
'
MsgBox "Done"
End Sub

Steven Craig Miller
 
B

Brian Smith

Steven, thanks for the code. Did exactly what I wanted. I have one other
question if you don't mind.

Concerning the number of characters, the code you provided gives the number
of characters including spaces. I've looked all over the place and can't
find the property that returns the number of characters without spaces.
Where does one find this information?

Brian
 
K

Klaus Linke

Brian Smith said:
Concerning the number of characters, the code you provided gives the
number of characters including spaces. I've looked all over the place and
can't find the property that returns the number of characters without
spaces. Where does one find this information?

Instead of newDoc.Characters.Count use
newDoc.ComputeStatistics(wdStatisticCharacters)

Regards,
Klaus
 
B

Brian Smith

Doug Robbins - Word MVP said:
See the article "Getting access to the Document Properties of a Word file"
at:

http://www.word.mvps.org/FAQs/MacrosVBA/DSOFile.htm

Doug, thanks for the link. This is a very useful tool!

However, I've run across a strange problem. For this time I've selected
"Number of pages" as one of the properties but I'm getting a result of "1"
for nearly every document (Word) I have in the folder selected when nearly
all of them have between 35 and 50 pages. Is this a known bug? The number of
characters and words properties appear to be working fine and match-up with
what Word reports internally.

I also have a request for an added feature if you happen to know who wrote
this add-in. It would be great if it was possible to multiselect properties
and select them all at the same time. Same for deselecting properties.

TIA for any help/hints on the above.

Brian
 

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