Solved

How to count words and characters within objects of word file

Posted on 2004-10-25
239 Views
Last Modified: 2012-05-05
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
0
Question by:micazone
    6 Comments
     
    LVL 2

    Expert Comment

    by:craigewens
    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
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    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
     

    Author Comment

    by:micazone
    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
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    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
     

    Author Comment

    by:micazone
    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
     
    LVL 16

    Accepted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now