Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limiting lines in a Word form field

Status
Not open for further replies.

JudyL

Instructor
Feb 21, 2001
148
US
Is there a way to check and see the number of lines that were entered in a form field in Word 2003. I have a text form field in a cell in a Word Table. I want to limit the number of lines so that the row does not exceed a certain height. I have limited the height of the row and the number of characters in the cell. However, if a person puts in just a few words on one line and then presses enter, the limit on the number of characters is useless. As the user continues to type, their text will not expand the row but it will disappear below the bottom of the cell. I would like to create a macro that would at least display a message telling the user that they have exceeded the number of lines. I don't want to count the paragraphs but I do want to count the lines.
 
JudyL,

Someone may know a way to limit the number of lines a form field can accept, but I know of no way to do that specifically. However, you can determine how many characters your field can take before it becomes too large; then, you can set that as the Maximum Length in the Text Form Field Options box. Once that is done, this problem should go away.

Of course, until they're properly educated, your USERS are likely to pitch a fit because they can't do things "their way", but that can be dealt with.

[glasses]

----------------------------------------------------------------------------------
"A committee is a life form with six or more legs and no brain." -- L. Long
 
Actually, I had already set a limit on the number of characters. The problem came in when someone typed a short sentence and then pressed enter. Because they had not hit the limit of the number of characters, they could easily have too many short lines.

I did find a way to limit it.
The code is:
Sub CheckLinesinField()
Dim vLines As Integer
ActiveDocument.Fields(10).Select
vLines = Selection.Paragraphs.Count

End Sub
Then I can add code and check that the lines are not more than what I need.

Thanks for replying. I think I solved my own problem.
 
I have in the past set the row height to exactly a certain height. This way if they keep typing it would not mess up the size of the table.

ck1999
 
JudyL,

"I don't want to count the paragraphs but I do want to count the lines."

As the issue is the user pressing Enter, in fact, you DO want to count the paragraphs, which is what you, in fact, do with your code.


vLines = Selection.Paragraphs.Count

Not sure why you are bothering to declare and use the vLines integer. It is not needed really. Also, is there a reason you are selecting the formfield?

faq219-2884

Gerry
My paintings and sculpture
 
Actually, I would like to count the lines but counting the paragraphs was the best I could come up with.

Can you suggest some better coding?
 
No, that sort of was my point. "Lines" can be fuzzy things. Paragraphs are not.

Besides: "The problem came in when someone typed a short sentence and then pressed Enter."

Well...pressing Enter does one thing. It makes...a paragraph mark. So really, that IS what you want to count.

I am not quite seeing the point, actually. You can do your test across a number of formfields, and yes, display a mesaage stating formfield Blah has "too many lines".

However, that will be rather obvious in the first place, yes? They are going to see that their text is going out os sight.

What exactly is the point to doing this?

Also, again, why are you selecting the formfield? There is no need to do so, and using Selection should generally be avoided.

Here is your code:
Code:
Sub CheckLinesinField()
Dim vLines As Integer
  ActiveDocument.Fields(10).Select
  vLines = Selection.Paragraphs.Count
End Sub

Here is an alternative:
Code:
Sub CheckField10() ' BTW: bad name....
Dim r As range
Set r = ActiveDocument.Formfields("Client").Range
If r.Paragraphs.Count > 7 Then
   Msgbox "Hey!  Your text is getting out of view!"
End If
End Sub

You have "Field(10)". Are there other fields? If it is a formfield, it would be better to use a formfield object.

You could also use a cell object for the table cell in question:
Code:
Dim aCell As Cell
Set aCell = ActiveDocument.Tables(1).Cell(2, 2)
If aCell.Range.FormFields(1).Range _
        .Paragraphs.Count > 2 Then
    MsgBox "Too many"
End If
Note that would NOT work for the "lines" issue. That is, if the user simply kept on typing. NOT pressing enter, just typing. The text would disappear below, but the paragraph count would not reflect this.

You can, in fact, check for lines using a Range. This method does not work on Selection. ComputeStatistics is not a method for Selection.
Code:
Dim aCell As Cell
Set aCell = ActiveDocument.Tables(1).Cell(2, 2)
If aCell.Range.FormFields(1).Range _
  .ComputeStatistics(wdStatisticLines) > 3 _
          Then
    MsgBox "Too many lines"
End If

Or, more simply:
Code:
If ActiveDocument.Formfields("Yadda").Range _
  .ComputeStatistics(wdStatisticLines) > 3 _
          Then
    MsgBox "Too many lines"
End If
If the range of the formfield has a computed statistic of lines greater than 3, then a message is displayed.

faq219-2884

Gerry
My paintings and sculpture
 

I've been thinking about this in odd moments since my last post. As Admiral Grace Hopper once said in a lecture (I'm paraphrasing), "There are no impossible problems; just programmers who haven't applied enough thought to those problems to find the solution."

As usual, she was right. This should solve your problem with the Rampant Return Writer:

Code:
'Remove ENTER characters
'Macro written:  3/21/08
'Author:  Walker Evans
Sub Kill_Return()
j = Selection.BookmarkID
ActiveDocument.FormFields(j).Select
    With Selection.Find
    .Text = "^p"
    .ClearFormatting
    .Replacement.Text = ""
    .Replacement.ClearFormatting
    .Execute Replace:=wdReplaceAll, Forward:=True
    End With
End Sub

You'll have to go into each of the Formfields and enter this as an Exit Macro, but that is really painless.

This will take any and all {RETURN} characters out of each FormField; since you're already limiting the number of characters per field, this should solve the entire problem.

Let me know if this works out for you.

[glasses]

----------------------------------------------------------------------------------
[small][ponder]"Did you hear about the guy who refused to pay his exorcist?[/small]
He was repossessed." [lol]
 
Here is a way to remove all Enters - as Walker's code does - but all at once. No Selection. No need to add as individual OnExit macros.
Code:
Sub KillReturns()
Dim DocFF As FormFields
Dim oFF As FormField

Set DocFF = ActiveDocument.FormFields
For Each oFF In DocFF
    If oFF.Type = wdFieldFormTextInput Then
        oFF.Result = Replace(oFF.Result, vbCr, "")
    End If
Next
End Sub

faq219-2884

Gerry
My paintings and sculpture
 

Gerry,

I originally had code that did the same thing (ie,remove them all at once when all entries were done), but before posting it I decided that doing it field-by-field might be better. If there are only one or two fields in JudyL's document the all-at-once solution is fine, but if there are a lot of fields it would be nice to rattle the users' up front rather than let them get all the way to the end before letting them know all their extraneous typing was wasted.

In any case JudyL now has both solutions and can pick whichever one works best in her application.

[glasses]


----------------------------------------------------------------------------------
[small][ponder]"Did you hear about the guy who refused to pay his exorcist?[/small]
He was repossessed." [lol]
 
Thanks to all of your for your help. That does work. Since I was creating this for others to use, they wanted the lines counted and this seems to satisfy them.

Let's count this issue as closed. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top