Discussion Locked

Character Count in MS Word

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm writing an app in VB (not a macro) that will, among other things, count the characters in a Word (97 or 2000) document according to corporate specified parameters. These parameters are still being clarified, so I may need to adjust them and recompile for analysis. So far, it looks like the standard will be identical with what comes with Word (such as: returns are not counted while spaces are) except tabs should be counted as characters. How can this be done using the MS Word object in VB6?
Thank you

5 replies

by glen_mcleod

This code uses the internal Word counter:

Function DocumentCharacters(ByVal sFile As String) As Long
Dim oWord As Word.Application
Dim oDoc As Word.Document

Set oWord = New Word.Application
Set oDoc = oWord.Documents.Open(sFile)

DocumentCharacters = oDoc.Characters.Count

oDoc.Close
Set oDoc = Nothing
Set oWord = Nothing
End Function

You have to set a reference to the Word object library in order to use the declarations "Word.Application" and "Word.Document"

Glen

by terrent

Ok, I got that. But the only changes in what gets counted are with or without spaces. I need to count tabs and possibly other stuff that MS doesn't already count.
Thanks anyway.

by andrew

Try this:
set a reference to the Word9 Object Library (MSWORD9.OLB)

add this code:

Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim lChars As Long
Set oApp = New Word.Application
Set oDoc = oApp.Documents.Open("c:\test.doc")
lChars = oDoc.ComputeStatistics(wdStatisticCharactersWithSpaces)
MsgBox lChars

by terrent

Ok, I got that. But the only changes in what gets counted are with or without spaces. I need to count tabs and possibly other stuff that MS doesn't already count.
Thanks anyway.

This discussion is closed to new replies.