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