How to count words and characters within objects of word file

How to count words and cgaracters of a ms word file including words and characters within the objects particularly Equation Editor/Math Type objects, text Boxes and inseretd excel sheet.
Thanks
micazoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

craigewensCommented:
Have you tried "ActiveDocument.Words.Count" ?
I haven't tested it with inserted objects before, but it might get you on the right track.
0
jimbobmcgeeCommented:
If it doesn't, you will need to iterate through all the objects in your document and take a word/character count of them, keeping a running total...

J.
0
micazoneAuthor Commented:
It does not count characters or words within objects like Math type.
Further I need a macro script for the same which can write the statistics in a csv text file.
Thanks for co-operation
S
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jimbobmcgeeCommented:
The simplest one would be:

       Sub WriteStatsToCSV(strDOCFileIn as String, strCSVFileOut as String)

              Dim oApp, oDoc
             
              Set oApp = CreateObject("Word.Application")
              Set oDoc = oApp.Documents.Open(strDOCFileIn)

              Open strCSVFileOut For Output As #1

                      Print #1, "Filename," & strDOCFileIn
                      Print #1, "Words," & oDoc.Words.Count
                      Print #1, "Characters," & oDoc.Characters.Count

              Close #1

      End Sub

Call this, in your program, with

      Call WriteStatsToCSV("x:\mypath\mydocfile.doc", "x:\mypath\mycsvfile.csv")

HTH

J.
0
micazoneAuthor Commented:
Good suggestion.
It works with single file.
I have to do it with multiple files and report of all files in one CSV file.
Thanks
0
jimbobmcgeeCommented:
The following should do all files in a folder you specify.  You'll need to add a reference to the Microsoft Scripting Runtime:

    Sub CheckAllFilesInFolder(strFolder As String, strOutFile As String)
   
        Dim oFSO        As Scripting.FileSystemObject
        Dim oFile       As Scripting.File
        Dim oOutFile    As Scripting.TextStream
        Dim oApp        As Word.Application
        Dim oDoc        As Word.Document
       
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set oApp = CreateObject("Word.Application")
                            'OPEN WORD
       
        If oFSO.FolderExists(strFolder) = False Then
            MsgBox "Folder not found: " & strFolder
            Exit Sub
        End If              'SIMPLE ERROR HANDLE IF FOLDER DOES NOT EXIST
       
        Set oOutFile = oFSO.CreateTextFile(strOutFile, True)
                            'OPEN TEXT FILE
       
            oOutFile.WriteLine "Filename, Words, Characters"
                            'WRITE CSV HEADER
       
            For Each oFile In oFSO.GetFolder(strFolder).Files
                            'CHECK ALL FILES IN FOLDER
           
                If UCase(oFile.Type) = "MICROSOFT WORD DOCUMENT" Then
                            'IF A WORD DOC
               
                    Set oDoc = oApp.Documents.Open(oFile.Path)
                            'OPEN IN WORD
                   
                    oOutFile.WriteLine oFile.Name & "," & _
                                       oDoc.Words.Count & "," & _
                                       oDoc.Characters.Count
                            'WRITE CSV VALUES
                                   
                    oDoc.Close False
                            'CLOSE DOC FILE
                   
                End If
       
            Next oFile
           
        oOutFile.Close      'CLOSE TEXT FILE
       
        oApp.Quit False     'CLOSE WORD
           
        Set oOutFile = Nothing
        Set oDoc = Nothing
        Set oApp = Nothing
        Set oFSO = Nothing  'CLEANUP
   
    End Sub

Call it with:

    Call CheckAllFilesInFolder("x:\mypath\mydocumentfolder", "x:\mypath\mycsvfile.csv")

HTH

J.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.