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?
 
jimbobmcgeeConnect With a Mentor Commented:
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

From novice to tech pro — start learning today.