Setting the maximum length of a text form field

P

PaddyPenfold

I'm creating a form using Word 2003, and one of my text fields needs to have
a maximum length of 30 words.

I can only set the maximum length in characters - can I set it to count words?
 
J

Jay Freedman

I'm creating a form using Word 2003, and one of my text fields needs to have
a maximum length of 30 words.

I can only set the maximum length in characters - can I set it to count words?

No, the field has no concept of "words". It's characters or nothing.

If you want add an exit macro to the field, you could write code that
counts the words in the field's result when the user tries to go to
the next field, and puts the cursor back into the field after showing
a message box. It would be similar to the macro in
http://www.word.mvps.org/FAQs/TblsFldsFms/ValidateFFields.htm. The
main problem with this approach is that most users will see a scary
antivirus message when they open the form, and some (who have their
security level set to High) won't be able to run the macro at all.
 
G

Graham Mayor

Assuming the field bookmark to be Text,1 running the following macro on exit
from that field should do the trick.

Sub MoreThan30()
Dim oFld As FormFields
Dim sText As String
Set oFld = ActiveDocument.FormFields
start:
sText = oFld("Text1").Result
oFld("Text1").Select
If Selection.Words.Count > 30 Then
sText = InputBox("Too many words - reduce to 30 or less", _
"Error", sText)
oFld("Text1").Result = sText
GoTo start:
End If
End Sub

http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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