PDA

View Full Version : Solved: Multiline Textbox Putting Square in Document



clhare
02-10-2007, 12:46 PM
Hi all!

I have a template that contains two multiline text boxes which allow the user to enter a complete address. The problem is that when the text is populated in the document, I get a little square at the beginning of each line (except the first) of the address. I have no idea why and can't figure out how to stop this from happening.

The only workaround I can think of is search for that square and removing it, but I don't know how to search for the square! I'd much rather stop it from getting inserted into the document anyway.

I've attached my test file.

Please help!

mdmackillop
02-10-2007, 02:25 PM
Hi Cheryl
A better method is to use bookmarks where you wish to insert your data, which can include the whole of the contents of a textbox. Here's a function from the KB to do this. The first part goes into your button code.
eg

' Replace variables with user's text
wb "Addressee", txtAddress1

The function
'Sub to enter text in Bookmark location
Private Sub wb(ByVal BName As String, ByVal inhalt As String)
'Steiner http://www.vbaexpress.com/kb/getarticle.php?kb_id=126
If ActiveDocument.Bookmarks.Exists(BName) Then
Dim r As Range
Set r = ActiveDocument.Bookmarks(BName).Range
r.Text = inhalt
ActiveDocument.Bookmarks.Add BName, r
Else
Debug.Print "Bookmark not found: " & BName
End If
End Sub

fumei
02-10-2007, 05:47 PM
Absolutely. Doing this as find and replaced text is not the way to go.

lucas
02-11-2007, 09:04 AM
That's a great way to handle userforms with lots of textboxes, combo's, etc. Thanks Malcolm for digging that up.

lucas
02-11-2007, 11:45 AM
Is there any way to retrieve the data back from the bookmark to a texbox on a userform using this function? I have been trying without any success.

mdmackillop
02-11-2007, 12:10 PM
Hi Steve,
On a userform with 2 textboxes and 2 buttons

Option Explicit

Dim bname As String

Private Sub CommandButton1_Click()
bname = "Addressee"
wb bname, TextBox1
End Sub

Private Sub CommandButton2_Click()
TextBox2.Text = ActiveDocument.Bookmarks(bname).Range.Text
End Sub

'Sub to enter text in Bookmark location
Private Sub wb(bname, ByVal inhalt As String)
'Steiner http://www.vbaexpress.com/kb/getarticle.php?kb_id=126
If ActiveDocument.Bookmarks.Exists(bname) Then
Dim r As Range
Set r = ActiveDocument.Bookmarks(bname).Range
r.Text = inhalt
ActiveDocument.Bookmarks.Add bname, r
Else
Debug.Print "Bookmark not found: " & bname
End If
End Sub

lucas
02-11-2007, 01:02 PM
I don't see and advantage to using the function getting the text back into a textbox. This seems to be just as easy......
Private Sub UserForm_Initialize()
Dim bmk As Bookmark
For Each bmk In ActiveDocument.Range.Bookmarks
If bmk.Name = "bookmarkname" Then
TextBox1.Text = bmk.Range.Text
End If
Next bmk
End Sub

mdmackillop
02-11-2007, 01:11 PM
Sorry Steve,
I just modified the earlier code to both write and retrieve the text. The retrieve code is simply
TextBox2.Text = ActiveDocument.Bookmarks(bname).Range.Text

lucas
02-11-2007, 01:29 PM
I follow it Malcolm...I was using a little different button 1 code for writing to mult txtboxes, combo's.
Private Sub CommandButton1_Click()
wb "bookmark1", TextBox1
wb "bookmark2", TextBox2
'wb "address", TextBox3
'wb "combo", ComboBox1
Unload Me
End Sub