PDA

View Full Version : Word counter for textbox



Prof-M
04-12-2013, 03:18 AM
Hi,

Is there any way that I can show number of characters while the user is filling the each textbox?

I tried to search the net but couldn't find anything.

Thanks

fumei
04-12-2013, 04:04 PM
Show where? In the Status bar? The following changes the text of the Statusbar each time the textbox text is changed - both adding OR removing.
Private Sub TextBox1_Change()
Application.StatusBar = "Textbox1 characters = " & Len(TextBox1.Text)
End Sub

Prof-M
04-12-2013, 05:02 PM
Show where? In the Status bar? The following changes the text of the Statusbar each time the textbox text is changed - both adding OR removing.
Private Sub TextBox1_Change()
Application.StatusBar = "Textbox1 characters = " & Len(TextBox1.Text)
End Sub


Thanks

It will be hard to notice in the status bar (there will be another 5 textboxes), I think the best place is beneath (or above) the textbox.

gmaxey
04-12-2013, 07:49 PM
You could use a lable caption propertry

fumei
04-12-2013, 09:31 PM
Greg, they are - at least in the document attached - legacy ActiveX textboxes IN the document. Unlike textboxes on a userform, these do not have a Caption property. And documents do not have labels.

"I think the best place is beneath (or above) the textbox."

Does that mean you would ADD a separeate paragraph (above or below), and put something like:

"The textbox below has X characters in it."

Seems weird. Personally I would be a little insulted to be told how many characters are in a textbox JUST below that statement.

gmaxey
04-12-2013, 09:38 PM
Should have looked at the attachment ;-)

fumei
04-12-2013, 09:40 PM
Amend that. Ahhh, they SEEM to be legacy ActiveX in my version of Word (XP). I am not entirely sure they are...I am getting some odd things happening.

Prof-M
04-13-2013, 01:26 AM
Thanks for the replies,

You may find the form weird, the reason is that most people (who is going to use it) are seniors in large organisations and normally they are using old version of MS Office and most of them "not so good" in dealing with PC.

The text limit is very important because part of the evaluation is to supply information within characters limit!

Prof-M
04-13-2013, 01:31 AM
Does that mean you would ADD a separeate paragraph (above or below), and put something like:

"The textbox below has X characters in it."


Yes, correct

gmaxey
04-13-2013, 07:05 AM
I would modify my form layout a bit (use a table and table cell for the count data). See attached.

fumei
04-13-2013, 12:22 PM
I use an older version of Word.

WHY is there a character limit? People are evaluated by the brevity of what text they put in the textbox?

If you can redo the form, then Greg's example is one way to go.

OR......you can retain your form, and use bookmarks. See attached. Code below.

Option Explicit

Sub UpdateBM(strBM As String, strText As String)
Dim r As Range
Set r = ActiveDocument.Bookmarks(strBM).Range
r.Text = strText
ActiveDocument.Bookmarks.Add Name:=strBM, Range:=r
End Sub

Private Sub TextBox1_Change()
Call UpdateBM("TB1", 100 - Len(TextBox1) & " characters remaining.")
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Len(TextBox1) = 100 Then
Beep
KeyAscii = 0
End If
End Sub

Private Sub TextBox11_Change()
Call UpdateBM("TB11", 1500 - Len(TextBox11) & " characters remaining.")
End Sub

Private Sub TextBox11_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Len(TextBox1) = 1500 Then
Beep
KeyAscii = 0
End If
End Sub

Private Sub TextBox111_Change()
Call UpdateBM("TB111", 1000 - Len(TextBox111) & " characters remaining.")
End Sub

Private Sub TextBox111_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Len(TextBox1) = 100 Then
Beep
KeyAscii = 0
End If
End Sub

BTW, you should start using Option Explicit. It will save your brain from frying.

This version adds a bookmark into the lines above each textbox (e.g. Title of the Project (Max 100 characters including spaces. BOOKMARK HERE). You NEED to add the Sub UpdateBM to your project in order to keep the bookmark when the count is added.

fumei
04-13-2013, 12:30 PM
Oh, and for future ease, try giving textboxes (or any other control) understandable names. Textbox111 is meaningless.

Prof-M
04-13-2013, 08:42 PM
I use an older version of Word.

WHY is there a character limit? People are evaluated by the brevity of what text they put in the textbox?

If you can redo the form, then Greg's example is one way to go.

OR......you can retain your form, and use bookmarks. See attached. Code below.

Option Explicit

Sub UpdateBM(strBM As String, strText As String)
Dim r As Range
Set r = ActiveDocument.Bookmarks(strBM).Range
r.Text = strText
ActiveDocument.Bookmarks.Add Name:=strBM, Range:=r
End Sub

Private Sub TextBox1_Change()
Call UpdateBM("TB1", 100 - Len(TextBox1) & " characters remaining.")
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Len(TextBox1) = 100 Then
Beep
KeyAscii = 0
End If
End Sub

Private Sub TextBox11_Change()
Call UpdateBM("TB11", 1500 - Len(TextBox11) & " characters remaining.")
End Sub

Private Sub TextBox11_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Len(TextBox1) = 1500 Then
Beep
KeyAscii = 0
End If
End Sub

Private Sub TextBox111_Change()
Call UpdateBM("TB111", 1000 - Len(TextBox111) & " characters remaining.")
End Sub

Private Sub TextBox111_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Len(TextBox1) = 100 Then
Beep
KeyAscii = 0
End If
End Sub

BTW, you should start using Option Explicit. It will save your brain from frying.

This version adds a bookmark into the lines above each textbox (e.g. Title of the Project (Max 100 characters including spaces. BOOKMARK HERE). You NEED to add the Sub UpdateBM to your project in order to keep the bookmark when the count is added.

Thanks, this is exactly what I need

The only issue now, I can't write-protect the document, anyway not big issue

fumei and gmaxey thanks again