PDA

View Full Version : Solved: TextForm Carriage Return



Marcster
01-15-2007, 12:15 PM
Word 2003

I have a document that I have with areas where a user enters text, eg. their name.
I have placed TextForm Fields on the document so they can fill out the document form and password protected the document.
Users can't click or change the documnet but when typing in the TextForm field and pressing Return it alters the rest of the document by moving the rest of it down.

As a workaround I put the form fields in table cells of fixed proportions.
So they will quickly see something is wrong when their text disappears.

How do I stop the Carriage Return messing up the TextForm field?. :banghead:
I've also limited the field to a certain number of characters.

Marcster.

mileski
01-15-2007, 01:09 PM
Is the control propery AutoSize False or True?

How about Scrollbars property ? Is it frmScrollBarsNone or ?
Multiline property ? True or False?

fumei
01-15-2007, 02:23 PM
Robert, it is not a ActiveX textbox control, it is a text formfield. It has no Multiline property, Scrollbar property, AutoSize property etc. etc.


How do I stop the Carriage Return messing up the TextForm field?
Simple answer is you can't. Limiting the number of characters is the best you can do. If you allow input (and of course you DO) then the Enter key is an allowable key to press. The Enter key will add a paragraph mark, like it always does. And therefore, add another line, therefore push text down.

You may, in fact, want to consider an ActiveX text control.

Or, perhaps use a Userform to get the user input, and validate the input there, before you put it in the document.

mileski
01-15-2007, 02:25 PM
Robert, it is not a ActiveX textbox control, it is a text formfield. It has no Multiline property, Scrollbar property, AutoSize property etc. etc.

I noticed that now :) Thanks for the correction Gerry.

mileski
01-15-2007, 03:59 PM
Word 2003

I have a document that I have with areas where a user enters text, eg. their name.
I have placed TextForm Fields on the document so they can fill out the document form and password protected the document.
Users can't click or change the documnet but when typing in the TextForm field and pressing Return it alters the rest of the document by moving the rest of it down.

As a workaround I put the form fields in table cells of fixed proportions.
So they will quickly see something is wrong when their text disappears.

How do I stop the Carriage Return messing up the TextForm field?. :banghead:
I've also limited the field to a certain number of characters.

Marcster.
Well Marcster, after a while thinking, i Solved your problem. It is doable without problems, and it works like a clock.

First, put this procedure in a Module in VBA in the Document:


Sub Clear_All_Enters()

Dim i As Integer
Dim aStr As Integer
Dim j As Integer
Dim fTxt As String

j = ActiveDocument.ProtectionType

If ActiveDocument.ProtectionType <> wdNoProtection Then _
ActiveDocument.Unprotect ("") 'put your protection password between the quotes

i = 1

ActiveDocument.Bookmarks("text1").Select

aStr = Len(Selection) - 1

Do While i < Len(Selection)
If Selection.Characters(i).Text = vbCr Then
Selection.Characters(i).Delete
i = 0
End If
i = i + 1
Loop

fTxt = Selection.Text

ActiveDocument.Protect Type:=j, Password:="" 'put your protection password between the quotes

Selection.Text = fTxt

Selection.Collapse Direction:=wdCollapseEnd

End Sub

Second, open the properties of the Form Field, and under Run Macro On -> Exit, choose "Clear_All_Enters",

Third, your document must be protected, in order this procedure to work corectly. So in the code above, if you have a password protecting your document, put it between the quotes marked in the code.


If this solves your problem, please mark the post as Solved.


ps..If you encounter any problems with the implementation, please post here.

Marcster
01-16-2007, 12:37 AM
Thanks for the replies guys.
Just searched Microsoft and the 'feature' for adding a line by presing Return in a form field was added by request of Word users.
http://support.microsoft.com/kb/134926/en-us

I'll test your code Robert and get back to you.

Marcster.

TonyJollans
01-16-2007, 01:40 AM
How do I stop the Carriage Return messing up the TextForm field?. :banghead:
I've also limited the field to a certain number of characters.

Just a warning about this. As discussed in another thread somewhere, Return characters are not necessarily counted when determining field length - I don't think anyone came up with a definitive set of criteria.

Marcster
01-16-2007, 11:22 AM
I've gone for the 'ActiveX textbox control'. So when a user presses the return key it goes to the next control.
Solved.

Marcster.

mileski
01-17-2007, 09:53 AM
I've gone for the 'ActiveX textbox control'. So when a user presses the return key it goes to the next control.
Solved.

Marcster.


Marcster, have you tested the code I gave you for the normal text control?

fumei
01-17-2007, 01:17 PM
mileski, there are some problems with your code.

1. At the end, protecting it with using NoReSet:= True will wipe out the original text.

2. Protecting it before making Selection.Text = fTxt causes an object error.

3. You declare and set a value for aStr:aStr = Len(Selection) - 1but never use it for anything.

4. You are looping through all the characters, just to find one specific thing. I added a counter to see how many iterations it required. A string with two vbCr, but 60 characters...takes 60 iterations. It has to loop through all of them.

5. As Tony mentions Return characters can be tricky with Len.

If I may suggest an alternative - even though the OP has gone to ActiveX....
Sub Clear_All_Enters()
Dim strIn As String

strIn = ActiveDocument.FormFields("Text1").Result
Do While InStr(strIn, vbCr) <> 0
strIn = Replace(strIn, vbCr, " ", 1)
Loop
ActiveDocument.FormFields("Text1").Result = strIn
End SubNotice this does not use Bookmarks, but Formfield.

Plus, there is no need to iterate through each character.

PLUS, there is no need to do any unprotection, reprotection.

mileski
01-17-2007, 02:25 PM
Hi fumei,

thanks for the correction with aStr. I put that string in the first version, but then didn't use anymore. It should be deleted.


And for your alternative, after the proposed solution, I agree, it is even a better solution.




1. At the end, protecting it with using NoReSet:= True will wipe out the original text.

2. Protecting it before making Selection.Text = fTxt causes an object error.


Well, I'm using Word 2003, and my code is working on my system. Don't know why you get that error.


Anyway, the problem is solved ;)

fumei
01-18-2007, 11:41 AM
Hmmm. Interesting. I wonder if 2003 has NoReSet=True as the default. 2002 has NoReSet=False as the default, so that when the doc is protected, the selection can not be put back in as the formfield values.

ctengelen
01-24-2007, 02:46 PM
***I wonder if 2003 has NoReSet=True as the default.*** Word 2003 has NoReset=False as the default setting.</p>