Counting Characters automatically

W

Wassim

Hello Developers

Is there a way in MS-Word 97 and up to count the number of characters
automatically in real time as they are being typed.

For example:

If I have a document that must be 2000 characters long, I would like
to count the characters so that when the user gets to 1990, I start
flashing messages for the user to stop typing soon before the text
becomes longer than 2000 characters.

I am hoping to be able to have this happen dynamically as the user
types, vs having the user hit a button to fire a macro and then the
count will come out.

So I think I need an event of a KeyDown or a keyboard buffer or any
help from you.

Thanks a lot.
 
J

Jezebel

Sadly, Word doesn't provide any keyboard events. It's possible to do this
through API calls to check the message queue, but that's seriously tricky
programming.

You could just run an endless loop with a DoEvents statement in it; it would
have some impact on performance depending on the power of your machine -- on
a slow machine it might make the keyboard seem a shade sluggish, and it
could cause problems if you have other macros.

Not sure how you're going to flash messages at the user, either. You can
popup forms or message boxes, but they require user-intervention to clear.
You can also use the status bar, but the user might well not notice. Or do
dramatic things like change the background color, but that will frighten the
horses.

Probably better -- and certainly simpler -- to check the count on
completion, when the user tries to do something with the document.
 
D

DA

Hi Wassim

There's no key-down event that triggers everytime the
user types a character as far as I'm aware. I'm fairly
sure that I read somewhere in the past that MS won't
include it because it would fire too often.

You can however achieve what you're after by using a form
field. Setup your document so that it contains a big text
box. Set the box properties so that you can type a large
amount ot text (ie. enable multi-line and scrollbars)

Now use the field change event to count your characters.
For example (Label for your textbox must be "TextBox1"
for the following code to work):

-----------
Sub TextBox1_Change()

If ThisDocument.TextBox1.TextLength >= 2000 Then
MsgBox "You've typed 2000 characters"
End If

End Sub
 
H

Helmut Weber

Hi,
have a look at this:
Sub CountIt()
If ActiveDocument.Characters.Count > 10000 Then
MsgBox "done"
Exit Sub
Else
StartiT
End If
End Sub
--
Sub StartiT()
Application.OnTime _
When:=Now + TimeValue("00:00:01"), _
Name:="CountIt"
End Sub
That does not contradict Jezebel's suggestion.
It's just a toy to play with,
but might be sufficient for your needs.
 

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