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

RE: Word/Character Count for a Group of Documents by stevencraigmiller(at)comcast(dot)net>

stevencraigmiller(at)comcast(dot)net>
Sat Jun 14 19:41:01 PDT 2008

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

"Brian Smith" wrote:

> 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
>
>
>

Re: Word/Character Count for a Group of Documents by Doug

Doug
Sun Jun 15 02:25:25 PDT 2008

See the article "Getting access to the Document Properties of a Word file"
at:

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


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"Brian Smith" <dcg_brian@hotmail.com> wrote in message
news:uSongHozIHA.548@TK2MSFTNGP06.phx.gbl...
> 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
>
>



Re: Word/Character Count for a Group of Documents by Brian

Brian
Tue Jun 17 11:44:59 PDT 2008

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

"StevenM" <stevencraigmiller(at)comcast(dot)net> wrote in message
news:65465553-3D23-427E-A426-A88B34643920@microsoft.com...
> 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
>
> "Brian Smith" wrote:
>
>> 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
>>
>>
>>



Re: Word/Character Count for a Group of Documents by Klaus

Klaus
Mon Jun 23 11:52:15 PDT 2008

"Brian Smith" <dcg_brian@hotmail.com> wrote:
> 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



Re: Word/Character Count for a Group of Documents by Brian

Brian
Tue Jun 24 20:58:24 PDT 2008

"Klaus Linke" <info@fotosatz-kaufmann.de> wrote in message
news:OR8RDJW1IHA.2064@TK2MSFTNGP05.phx.gbl...
> "Brian Smith" <dcg_brian@hotmail.com> wrote:
>> 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

Thanks Klaus.

Brian



Re: Word/Character Count for a Group of Documents by Brian

Brian
Tue Jun 24 21:06:54 PDT 2008

"Doug Robbins - Word MVP" <dkr@REMOVECAPSmvps.org> wrote in message
news:eMjUBnszIHA.5892@TK2MSFTNGP02.phx.gbl...
> See the article "Getting access to the Document Properties of a Word file"
> at:
>
> http://www.word.mvps.org/FAQs/MacrosVBA/DSOFile.htm
>
>
> --
> Hope this helps.

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