Count Words In a Folder

J

Jan Kratochvil

I have about 20 DOC in one folder and I need to know how to cout all words
with spaces in this 20 DOC files.

Exist any useful Macro?

Thank you
 
D

David Sisson

Copy this to a blank document/module 1

Sub CountAllWords()
'2007 David Sisson
Dim aDoc As Document
Dim bDoc As Document
Dim sFolderToLookIn As String
Dim TotalWords As Integer
Dim DocWordsCount As Integer
Dim X As Integer

Set bDoc = ActiveDocument
sFolderToLookIn = "C:\My Documents\CountingFolder"
With Application.FileSearch
.NewSearch
.FileName = "*.doc"
.LookIn = sFolderToLookIn
.Execute
For X = 1 To .FoundFiles.Count
Application.ScreenUpdating = False
Set aDoc = Documents.Open(.FoundFiles(X))
DocWordsCount =
aDoc.ComputeStatistics(Statistic:=wdStatisticWords, _
IncludeFootnotesAndEndnotes:=True)

bDoc.Range.InsertAfter .FoundFiles(X) & " - " & DocWordsCount
& vbCr
TotalWords = TotalWords + DocWordsCount
aDoc.Close savechanges:=wdDoNotSaveChanges
Next X

bDoc.Range.InsertAfter TotalWords & " Total words in " & X & "
documents."
End With
Application.ScreenUpdating = True
End Sub
 
J

Jan Kratochvil

Thanks but I have compile error on line with text:
documents."
-----------

Sub CountAllWords()
'2007 David Sisson
Dim aDoc As Document
Dim bDoc As Document
Dim sFolderToLookIn As String
Dim TotalWords As Integer
Dim DocWordsCount As Integer
Dim X As Integer

Set bDoc = ActiveDocument
sFolderToLookIn = "C:\My Documents\CountingFolder"
With Application.FileSearch
.NewSearch
.FileName = "*.doc"
.LookIn = sFolderToLookIn
.Execute
For X = 1 To .FoundFiles.Count
Application.ScreenUpdating = False
Set aDoc = Documents.Open(.FoundFiles(X))
DocWordsCount = aDoc.ComputeStatistics(Statistic:=wdStatisticWords,
_
IncludeFootnotesAndEndnotes:=True)

bDoc.Range.InsertAfter .FoundFiles(X) & " - " & DocWordsCount & vbCr
TotalWords = TotalWords + DocWordsCount
aDoc.Close savechanges:=wdDoNotSaveChanges
Next X

bDoc.Range.InsertAfter TotalWords & " Total words in " & X & ""
documents."
End With
Application.ScreenUpdating = True
End Sub
 
H

Helmut Weber

Hi Jan,

it is probably only about line breaks
caused by simply copying code from the newsreader.

DocWordsCount = aDoc.ComputeStatistics(Statistic:=wdStatisticWords,
IncludeFootnotesAndEndnotes:=True)

You may try:

DocWordsCount = _
aDoc.ComputeStatistics(_
Statistic:=wdStatisticWords, _
IncludeFootnotesAndEndnotes:=True)

Note, that there is a space after each underscore.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
R

Russ

Jan and Helmut,
That should be space, then underscore for line continuation.
---------------
From VBA Help:
line-continuation character
The combination of a space followed by an underscore (_) used in the
development environment to extend a single logical line of code to two or
more physical lines. However, you can't use a line-continuation character to
continue a line of code within a string expression.
---------------
Therefore:
DocWordsCount = _
aDoc.ComputeStatistics( _
Statistic:=wdStatisticWords, _
IncludeFootnotesAndEndnotes:=True)
 
R

Russ

Jan,
You get around that within the string limitation by splitting and
concatenating the string in strategic locations and using the line
continuation character outside of the string.

Change:
MyString = "However, you can't use a line-continuation character to
continue a line of code within a string expression."

To:
MyString = "However, you can't use a line-continuation" _
& " character to " & _
"continue a line of code within a string expression."

Or: Indent every continuation line after the first to show how they are
linked to the first line.

MyString = "However, you can't use a line-continuation" _
& " character to " & _
"continue a line of code within a string expression."
 
H

Helmut Weber

Hi Jan,

Office 2007 is sending us back to the roots.

For getting a list of all files below
the directory "c:\test\" with filename extension "doc",
use in the command shell

dir c:\test\*.doc /s/b > c:\testdir.txt

In c:\testdir.txt you'll find e.g.

c:\test\brownfox-02.doc
c:\test\Doc 2.doc
c:\test\doc.doc
c:\test\ThisDoc-001.doc
c:\test\Excel\10.doc
c:\test\Excel\tables-in-tables.doc
c:\test\Word\011.doc
c:\test\Word\016.doc

ready for being processed.

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
R

Russ

Desperate times call for desperate measures.
DirDrill might be a substitute for filesearch. I am not using Word2007, so I
can't test. Basically, you can unzip the file and import the class code,
form code, and visual basic code into your project and then adapt the
filesearch code given to you for word count, to use DirDrill instead.
<http://vb.mvps.org/samples/project.asp?id=dirdrill>

Maybe Karl (DirDrill author), if he is reading this, can comment or quickly
adapt the aforementioned code to use the DirDrill properties and methods for
us.
 

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