PDA

View Full Version : UserForm TextBox odd behaviour when assigned to a FormField



Strongs
09-11-2007, 09:57 AM
Hi Guys,

I have a document that has a UserForm with TextBoxes. When the user completes the UserForm, the TextBox contents are assigned to a FormField in the document. The code I am using works fine, but the when the text is inerted into the FormField there seems to be odd behaviour.

The FormField is set to be a numbered list with alpha characters, so if the user starts a new paragraph in the UserForm TextBox by hitting Return Key, the subsequent text entered into the FormField when the UserForm is submitted, will increment by x number of alpha characters i.e. para 1 = a. para2 = b. etc;

I have tried this by manually entering the text into the FormField and it works fine: hit the return key at the end of a paragraph and as expected the a paragraph starts on a new line with an incremented alpha character.

However, when the UserForm TextBox is submitted this is not happening. The additional paragraph(s) simply wrap underneath the first paragraph alpha.

Additionally, there was a problem with a square box symbol appearing on the next paragraph line when submitted through the UserForm, but after some research on the net found that this was down to Chr(10) causing this problem. The TextBox seems to be set to CrLf when it is submitted.

I have solved this problem by using the Replace function to remove the erroneous Chr(10) and hey presto, no more square box symbol.

The code I am using is shown below and attached is a document with images of what is occuring when the UserForm is submitted.

It is driving me crazy, so any help that you can offer would be greatly appreciated. The document is good to go apart from this annoying problem.

Regards,

If Me.Interest = "" Then
ActiveDocument.FormFields("Int").Result = "Enter info here"
Else
ActiveDocument.FormFields("Int").Result = Replace(Interest, Chr(10), "")
End If

fumei
09-11-2007, 10:41 AM
To get a different number, you need a different paragraph. Try this.
ActiveDocument.FormFields("Int").Result = Replace(Interest, Chr(10), Chr(13))

Strongs
09-11-2007, 12:16 PM
Hi Gerry,

Thanks for coming back to me so soon. I have tried your suggestion, but the same effect occurs but with an additional paragraph between the two lines of text. I have noticed that the same effect can be simulated manually by inserting a soft return (shift+return). Is there an ascii character for this?

Regards,

Paul

Strongs
09-12-2007, 09:18 AM
Hi,

I think I have solved the problem! After looking at a couple of other threads on the site and modifying some code, the code below seems to work. I don't know why it works given that (I assume) [^13]=Chr(13) and ^p = Chr(13).

Perhaps one of the more experienced VBA gurus could enlighten me because I can't see the logic.

Thanks to those who have attempted, or are attempting to find a solution to this problem.

Dim myFld as Long

Application.ScreenUpdating=False
If Me.Interest = "" Then
ActiveDocument.FormFields("Int").Result = "Enter info here"
Else
ActiveDocument.FormFields("Int").Result = Replace(Interest, Chr(10), "")
End If

ActiveDocument.Unprotect Password:="Blah"
For myFld = 1 To ActiveDocument.FormFields.Count
ActiveDocument.FormFields(myFld).Select
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[^13]"
.Replacement.Text = "^p"
.Forward = True
.Wrap = False
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next myFld
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True, Password:="Blah"
Application.ScreenUpdating = True