Return (or Enter, or NewLine) displayed as ◙◙ in field result

C

Cooz

Hi everyone,

I have a userform in which users can type an address in a multiline textbox
that has EnterKeyBehavior set to True. Code in the form assigns the value of
the textbox to a document variable:

Activedocument.Variables("Address").Value = Me.txtAddress.Value

When I display the value of this document variable with a DocVariable field,
the result is

Name◙◙Address◙◙City

Where I would like to have

Name
Address
City

Is there any way to achieve this result?

Thank you,
Cooz
 
F

Fumei2 via OfficeKB.com

I assume you also have MultiLine set = True.

If EnterKeyBehavior = True
MultiLine = True

then

If:

Yadda
Blah
Whatever

is entered into Textbox1 and a commandbutton executes:

Private Sub CommandButton1_Click()
ActiveDocument.Variables("Test").Value = TextBox1.Text
Unload Me
End Sub

If you have a DOCVARIABLE field as:

{ DOCVARIABLE "Test" */MERGEFORMAT }

then it shows as - using ASCII for the non-text characters):

Yadda Chr(13) - Yadda CR
Chr(10) Blah Chr(13) - LF Blah CR
Chr(10) Whatever Chr(13)

In other words, yes, it does put the three lines on three lines, but the
second and third lines have the additional LF at the start.

This can be fixed (and there may be other ways) by:

Option Explicit

Private Sub CommandButton1_Click()
ActiveDocument.Variables("Test").Value = _
Replace(TextBox1.Text, Chr(10), "")

ActiveDocument.Range.Fields(1).Update
Unload Me
End Sub


Replacing the Chr(10) with "" (nothing)

This ends up with

Yadda
Blah
Whatever
 
P

Peter Jamieson

There are potentially at least two problems here:

Some versions of Word display chr(10) (Line Feed) as a block character,
and others do not. As Fumei2 says, you can workaround this by removing
chr(10) from the Variable value. I believe this behaviour may have
changed even within versions of Word as a consequence of hotfixes/SPs

Word 2002 and Word 2000 have an additional problem where both chr(10)
and chr(13) both display as block characters when the [ DOCVARIABLE } is
in a table cell. AFAIK the only fix for that is to ensure that the cell
has a carriage return at the end of the cell. See e.g.

http://support.microsoft.com/kb/283025

In addition, docvariables have been misbehaving in some versions of Word
2007 when you save as .docx. See e.g.

http://support.microsoft.com/kb/970226

(and for other misbehaviour it is probably worth searching
support.microsoft.com for "docvariable")

All of which makes it somewhat easier to work with docvariables when
they only contain fairly short, single-line text.

Peter Jamieson

http://tips.pjmsn.me.uk
 
C

Cooz

Hi Fumei2,

I still get the wrong result when I try your proposal. I actually use this
code:

Dim ctl As Control
For Each ctl In Me.Controls
Select Case Left(ctl.Name, 3)
Case "txt", "lst", "cmb" ' my TextBoxes, ListBoxes and
ComboBoxes have these prefixes
Lib.SetDocVar Mid(ctl.Name, 4), Replace(Trim(ctl.Value),
Chr(10), "")
Case Else
End Select
Next ' ctl

My form contains a TextBox "txtAddress"; the document contains a DocVariable
field { DocVariable Address \* MERGEFORMAT }.

When I update the DocVariable fields, the result is, for example,
Nameâ—™Addressâ—™City
- still on the same line.

I use Word 2003.

Cooz
 
C

Cooz

Hi Fumei2,

This does the trick:
Replace(Trim(ctl.Text), Chr(13) & Chr(10), Chr(11))

Thank you for thinking along.

Cooz
 

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