Count Characters

S

Senad Isanovic

How to count a number of characters (including spaces) in the word document?
I need a VBA function that will count characters during the time you are
writing. If the number of characters is more then x - then display the
message, save and close the doc. The documents can contain pictures and tabs
and tables but these should not be included when you count the characters.
Thanks!
 
H

Helmut Weber

Hi Senad,
possible, in a way, and complicated. But what would it be good for?
You need the ontime-methode, a macro that permanently counts
characters. And then process all tables and count the characters
within their ranges. And then count all tabs in the doc! And deduct
the number of characters in tables plus the number of tabs form all
characters. And in between the user, who might be an extremely fast
typing secretary, might have continued typing...
Very strange.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
S

Senad Isanovic

It sounds strange, I know, but I still need that vba code. So if you have
some sample code for how to check the number of characters I'd be very
thankful if you submit it. /Senad
 
H

Helmut Weber

Hi Senad,
ok, if it really has to be, but this is not a bulletproof
thing, rather a demonstration, how it would work,
if nothing unforseen happens.
I am assuming, you can handle saving and closing yourself.
I set the interval to 5 seconds and the maximum characters
in the doc to 2800. Improvements welcome.
Let us know if you need further assistance.
---
Sub TestTime()
Application.OnTime _
When:=Now + TimeValue("00:00:05"), _
Name:="Countchar"
End Sub
---
Sub CountChar()
Dim lChrDoc As Long ' characters in document
Dim lChrMax As Long ' max characters in document
Dim lChrTbl As Long ' characters in tables
Dim lChrTab As Long ' characters that are tabs
Dim oTbl As Table ' object table
Dim oRng As Range
lChrMax = 2800
Set oRng = ActiveDocument.Range
' --- characters in tables
lChrTbl = 0
For Each oTbl In ActiveDocument.Tables
lChrTbl = lChrTbl + oTbl.Range.Characters.Count
Next
' --- number of tabs
Resetsearch
lChrTab = 0
With oRng.Find
.Text = "^t"
.Replacement.Text = "^t"
While .Execute
lChrTab = lChrTab + 1
Wend
End With

lChrDoc = ActiveDocument.Characters.Count
If lChrDoc - (lChrTab + lChrTbl) > lChrMax Then
MsgBox "enough"
' save, close
Else
TestTime
End If

End Sub
 
H

Helmut Weber

....
Sub Resetsearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
 
L

Larry

If Helmut's macro works, fine. But if there are problems with it, this
simplified macro, which displays a message books just showing the number
of characters and spaces in the document (not, like the built-in Word
Count dialog, a whole list of parameters) might be useful.


' Code so that running this won't prompt a "Save?" message.
Dim bSaveState As Boolean
bSaveState = ActiveDocument.Saved

Application.ScreenUpdating = False
System.Cursor = wdCursorIBeam
MsgBox "Characters (including spaces) in document " & _

Format(ActiveDocument.ComputeStatistics(wdStatisticCharactersWithSpaces)
, _
"#,##0"), vbOKOnly, "Character Counter"
' If there is selection, display Word's built in WordCount dialog:
ActiveDocument.Saved = bSaveState

Larry
 
S

Senad Isanovic

Thanks very much for taking your time! /Senad

Larry said:
If Helmut's macro works, fine. But if there are problems with it, this
simplified macro, which displays a message books just showing the number
of characters and spaces in the document (not, like the built-in Word
Count dialog, a whole list of parameters) might be useful.


' Code so that running this won't prompt a "Save?" message.
Dim bSaveState As Boolean
bSaveState = ActiveDocument.Saved

Application.ScreenUpdating = False
System.Cursor = wdCursorIBeam
MsgBox "Characters (including spaces) in document " & _

Format(ActiveDocument.ComputeStatistics(wdStatisticCharactersWithSpaces)
, _
"#,##0"), vbOKOnly, "Character Counter"
' If there is selection, display Word's built in WordCount dialog:
ActiveDocument.Saved = bSaveState

Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top