Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi line textbox

Status
Not open for further replies.

KatSoft

Programmer
Jun 26, 2003
7
0
0
BE
This is the problem : I use a UserForm to gather information from the user. This information is typed into a textbox on the userform and the user can create multiple lines in that textbox.
So far, no problems. The trouble starts when I write code in VBA to copy the content of that textbox into a FormField in a Word document. Instead of multiple lines, I get 1 line. Instead of carriage returns, there are unreadable signs inside the text (which manifest themselves as little squares).

This is the code I use to transfer the text :
ActiveDocument.FormFields("ProdDescr").Result = Me!txtProdDescr
This code should transfer the text in the textbox txtProdDescr (on the UserForm) to the FormField ProdDescr (on the Word document).
It works, but I lose the multiple line formatting.

Can anyone help me with this?
 
your code is grabbing the entire contents of the text box and passing it as a single string.

You might have to send each line of the text box as it's own string.
 
Etid, you might be right but it leaves the problem for me :
How do I get the separate lines into the formfield on the document so that the line format is preserved?

I could use the Split function to create the separate lines but how do I transfer them into the form field?
 
Have you tried this ?
ActiveDocument.FormFields("ProdDescr").Result = Replace(Me!txtProdDescr, vbLf, vbCrLf)
or some variation.


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
to PHV

Yes, I've tried the Replace function as well.
The problem seems to be that you cannot write a vbCrLf to a formfield. I mean, you can write a new line to the field but it does not seems to actually create a new line. You just see "...line 1..." and "...line 2 ..." with 2 squares between them but written on 1 line. The odd thing is, when you transfer those squares to a textfield on a form, they create a new line!
Really puzzling....
I still can't get it to work.
 
Hi KatSoft,

I think PH has the right idea but not quitethe right solution.

In a Textbox, CR/LF combinations are used for new lines. In a Form Field, CR characters alone are used, so try ..

[blue][tt]ActiveDocument.FormFields("ProdDescr").Result = Replace(Me!txtProdDescr,vbCrLf,vbCR)[/tt][/blue]

.. and the opposite to move the text back.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Tony,
Thanks for the nice try but I'm sorry to tell you it does not work either.
Instead of 2 squares in the text, I only have one but it still does not split my lines.
I think the squares are Char(13) and Char(10) that do not get converted into their "command", being Carriage Return (Char13 or vbCR) and Line Feed (Char10 or vbLF)
This would explain why your code gives me only 1 square (being an unprocessed Char13 or Carriage Return).

So the problem remains....
Someone has a bright idea?
 
Hi KatSoft,

Interesting. I just knocked up a quick test (with a regular form with a regular textbox and a regular text form field in the document, no changes from the defaults - moving the text in the form's click event) and when I do what you first posted, it worked fine, doing the conversion without any help from me. Don't know what else to suggest at the moment.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
I lied! I did change the defaults - to make the text box (on the form) MultiLine - but nothing else!

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Tony,

the properties of the textbox do not influence anything. Even if I use simple code like :

ActiveDocument.FormFields("ProdDescr").Result = "First line" & vbCrLf & "Second line"

the text that arrives in the form field is not split into 2 lines. So the problem should be in the form field. But there are no properties there that I can set to make sure the form field is multiline. The formfield is always multiline : if you enter directly in it, you can create two or more lines by simply pressing Enter or Shift Enter.

I use Word 2000. Anything different for your version?

KatSoft
 
Hi KatSoft,

Tony's approach definitely seems good - but as far as I know, Word uses chr(10), i.e. LF as soft return, not CR.

Try to determine the line separating character from the textbox first, e.g. with
Code:
Dim delim
Select Case true
 Case instr(1,Me!txtProdDescr,vbLF)>0 and instr(1,Me!txtProdDescr,vbCR)=0
   delim= vbLF
 Case instr(1,Me!txtProdDescr,vbCR)>0 and instr(1,Me!txtProdDescr,vbLF)=0
   delim= vbCR
 Case instr(1,Me!txtProdDescr,vbCrLf)>0
  delim= vbCrLF
 Case Else
  msgbox "neither"
End Select
...
If delim<>"" then ActiveDocument.FormFields("ProdDescr").Result = Replace(Me!txtProdDescr, delim, vbLf)

If you receive the messagebox, you might have to cheat:
determine, how many characters can be entered before a line break occurs and use the DIV operator to split the text in suitable chunks, each followed by vbLf.

Hope this helps,
Andy


[blue]The last voice we will hear before the world explodes will be that of an expert saying:
"This is technically impossible!" - Sir Peter Ustinov[/blue]
andreas.galambos@bowneglobal.de
HP:
 
really nice Andy, but it's not resolving anything.

In a textbox as well as in Word, I manage to filter out the needed characters for a new line : they both are vbCrLf (=Char13 + Char10)

I simply do not succeed in inserting in a form field a text with multiple lines. I've tried :
ActiveDocument.FormFields("ProdDescr").Result = "First line" & vbCrLf & "Second line"
and then again but with vbCr alone or with vbLF alone or....
Nothing succeeds!

I know it must be vbCrLf : if i sent text with that separator to a form field, I can see the 2 squares representing the line separator vbCrLf. When I then copy the content of the form field and paste it into another (by hand in Word, not with code!) form field, the lines are separated as they should be.
So the problem remains : I simply can't write multiple lines into a form field when I use VB code....

But thanks anyway for the effort!

greetz,
KatSoft
 
the "field" is probobly single line.
i know that wasn't much help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top