Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2006

Tip: Looking for answers? Try searching our database.

How do I check word count of multiple word documents?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sujal - 25 Feb 2006 04:51 GMT
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!
Cooz - 27 Feb 2006 14:00 GMT
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
--
PS: If this is a satisfying answer to your question and you're logged in via
the Microsoft site, please click Yes to "Did this post answer the question?".
Thanks.

> 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!
Sujal - 28 Feb 2006 01:30 GMT
Thanks very much!

> Hi Sujal,
>
[quoted text clipped - 91 lines]
> > 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!

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.