Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to count words and characters within objects of word file

Posted on 2004-10-25
6
Medium Priority
?
241 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
Comment
Question by:micazone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:craigewens
ID: 12408665
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
ID: 12408904
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
ID: 12409548
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12410330
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
ID: 12412460
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:
jimbobmcgee earned 1000 total points
ID: 12459946
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

636 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