MultiLine Textbox

K

Kevin R

I have a userform with a multiline textbox, the EnterKeyBehavior set to True
and MultiLine is set to True. This form is used to create a letter and the
multiline field allows the user to enter names for a cc field on the letter.
If more than 1 cc name is needed, the user can press the enter key while in
the multiline textbox and enter the next name until all names are entered.
However, when the letter is generated, the names all appear on the same line
rather than starting each name on a new line. I cannot figure this out.
Below is the code for the cc on the userform that stores it as a variable.
In the letter, I then use the { DOCVARIABLE "CC" } to call the value from the
userform. Any ideas how to preserve the carriage returns entered in the
field on the user form?

If txtCC.Value <> "" Then
.Variables("CC").Value = txtCC.Text
Else
.Variables("CC").Value = " "
End If
 
J

Jean-Guy Marcil

Kevin R said:
I have a userform with a multiline textbox, the EnterKeyBehavior set to True
and MultiLine is set to True. This form is used to create a letter and the
multiline field allows the user to enter names for a cc field on the letter.
If more than 1 cc name is needed, the user can press the enter key while in
the multiline textbox and enter the next name until all names are entered.
However, when the letter is generated, the names all appear on the same line
rather than starting each name on a new line. I cannot figure this out.
Below is the code for the cc on the userform that stores it as a variable.
In the letter, I then use the { DOCVARIABLE "CC" } to call the value from the
userform. Any ideas how to preserve the carriage returns entered in the
field on the user form?

What Word version?
In Word 2003, right now, the "Enter" character generated in a mutliline text
box is translated as Chr(13) and Chr(10). So instead of everyting on the same
line, you should get superfluous spaces (from the Chr(10)) and as many lines
as you have lines in the userform. In any case, you need code to replace the
Enter character from the userform, something like:

Dim strText As String

With Me
If txtCC.Value <> "" Then
strText = Replace(.txtCC.Text, Chr(13) & Chr(10), Chr(13))
ActiveDocument.Variables("CC").Value = strText
Else
ActiveDocument.Variables("CC").Value = " "
End If
End With
 
K

Kevin R

Still no luck. I'm using Word 2003 SP2. When I tried the code you
suggested, I still get the same results. For example if I type Bill (press
Enter) and type Bob, I get:

Bill Bob

on the same line. The cursor will move 2 spots between the names but they
appear to be slightly larger than just your normal space. Odd thing is if I
copy the line from the document and paste into a new blank document I get
something like:

Bill
Bob
 
J

Jean-Guy Marcil

Kevin R said:
Still no luck. I'm using Word 2003 SP2. When I tried the code you
suggested, I still get the same results. For example if I type Bill (press
Enter) and type Bob, I get:

Bill Bob

on the same line. The cursor will move 2 spots between the names but they
appear to be slightly larger than just your normal space. Odd thing is if I
copy the line from the document and paste into a new blank document I get
something like:

Just to make sure we are talking about the same thing... Insert the
following two lines of code:
MsgBox Asc(Mid(txtCC.Value, 5, 1))
MsgBox Asc(Mid(txtCC.Value, 6, 1))
after
If txtCC.Value <> "" Then

And test the code using your example "Bill(Enter)Bob"

See what you get.
 
K

Kevin R

I'll give that a try and let you know. I think part of the problem may be
that the docvariable is inside a Word table. If I use the docvariable
outside the table, it displays correctly.
 
J

Jean-Guy Marcil

Kevin R said:
I'll give that a try and let you know. I think part of the problem may be
that the docvariable is inside a Word table. If I use the docvariable
outside the table, it displays correctly.

I do not know why... But in a table make sure you have a "real" ¶ at the end
of the DOCVARIABLE field. If that screws up the formatting, format that ¶ and
the following cell marker ¤ to a size of 1.
 
D

Doug Robbins - Word MVP

Here is another method that I have resorted to when I did not want to have a
paragraph mark after the last entry.

Dim CC As String
If txtCC.Text <> "" Then
strCC = txtCC.Text
With .Bookmarks("CC").Range.Rows(1).Cells(2).Range
If InStr(strCC, Chr(13)) = 0 Then
.InsertAfter strCC
Else
i = 1
Do While InStr(strCC, Chr(13)) > 0
If i = 1 Then
.InsertAfter Left(strCC, InStr(strCC, Chr(13)) -
1)
strCC = Mid(strCC, InStr(strCC, Chr(13)) + 2)
i = 2
Else
.InsertAfter vbCr & Left(strCC, InStr(strCC,
Chr(13)) - 1)
strCC = Mid(strCC, InStr(strCC, Chr(13)) + 2)
End If
Loop
.InsertAfter vbCr & strCC
End If
End With
End If


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jean-Guy Marcil

Doug Robbins - Word MVP said:
Here is another method that I have resorted to when I did not want to have a
paragraph mark after the last entry.

But this would not work with a DOCVARIABLE field (that the poster is using),
right?
 
D

Doug Robbins - Word MVP

That's correct.

I came up with the method when engaged on a job to modify some templates
created by some consultants (whose name I shall not mention) in the UK who
inserted information/constructed tables in the document by setting a range
variable to the .Range of the document, then collapsing it and inserting
what they wanted then repeating the process. For some reason, when I tried
to change the line spacing of the empty paragraph inserted after the
docvariable field, it had a detrimental effect on other parts of the
document that had been built by their code.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top