I came across a curious behavior of VBA in changing the TextBox control's CurLine property.
This property behaves as expected when automatic word wrapping occurs: If the next character entered causes the current word to we wrapped onto the next line, VBA increments the value of CurLine to indicate that the insertion point is now positioned on the next line.
However, if the user presses <Enter> to insert an explicit line break into the text (the character pair vbCrLf), VBA does *not* increment the value of CurLine, even though the insertion point is now positioned on the next line. Instead, an additional character must be typed before CurLine gets incremented.
To illustrate, create a 2-line high TextBox control (TextBox1) with its MultiLine property set to True.
Double-click the control to create a Change event handler. Add this single line to the handler:
If Me.TextBox1.CurLine > 0 Then Beep 'beep if 2 lines
Now run the form and enter a few characters in TextBox1. No beeps. Now press Enter. Still no beep!, even though the insertion pointer is now sitting on line 2. Now enter another character. Beep!
This means that your Change event handler will not always detect a line overflow condition at the precise moment when it occurs, in this case when the user pressed Enter to insert a new line. Worse, if the user presses Enter to add a new line to the end of the text, but never enters any text on that line, the handler will not detect the new line.
Note that the handler *does* work correctly in detecting a line overflow condition in cases where an automatic word wrap moves the insertion pointer to the next line.
To demonstrate this, run the form again and start typing characters on the first line. At the moment the insertion point moves to the second line, you'll hear a beep.
Submitted via EggHeadCafe - Software Developer Portal of Choice
SharePoint Lists In Excel Via VSTO
http://www.eggheadcafe.com/tutorial...8377c/sharepoint-lists-in-excel-via-vsto.aspx