View Full Version : [SOLVED] userform text box formatting and exporting to word template

10-06-2015, 09:47 AM
Hi everyone,

I have a userform with an "address" text box. Don't know if it makes a difference but the userform was created through excel.
I want the user to be able to use the return key in the text box to have the address appear as follows (on the userform):

John Wayne
123 Easy St
Pocohontas, NE

I would like to then copy the address to the document in the same format.
I can get the text to copy from the userform to the document, just not with the desired formatting. Any ideas? Here is the code I am currently working with:

Private Sub btn_Continue_Click()

Dim wrdApp As Object
Dim wrdNNDF As Object

Set wrdApp = CreateObject("Word.Application")
Set wrdNNDF = wrdApp.Documents.Open("C:\template.doc")


wrdNNDF.doc_RR_tbox_address.Text = TESTMASTER_optbut.tbox_address.Text

wrdNNDF.Close SaveChanges:=wdDoNotSaveChanges
Unload Me
End Sub

10-06-2015, 04:26 PM
It's easier to use
In both Excel and Word UserForms and documents

With wrdNNDF
.doc_RR_tbox_Name = Me.tbxName
.doc_RR_tbox_Add1 = Me.tbxAdd1
End With

strAddress = tbxName & vbCrLf & tbxAdd1 & vbCrLf
If Not tbxAdd2 = "" Then strAddress = strAddress & tbxAdd2 & vbCrLf
strAddress = strAddress & tbxCity & ", " & tbxState & vbCrLf & tbxZip

'Something = strAddress

That may not be feasible, I don't know how you are setting the value of tbox_adddress. I suspect that the linefeed character(s) used in tbox_address are not being recognized by Word's address box.Somehow, you will have to discover which linefeed chars are used in tbox_address and which one Word's address box uses and match them up.

try this in

Sub tbox_address_AfterUpdate()
MsgBox "vbCr " & Instr(tbox_address, vbCr) > 0
MsgBox "vbLf " & Instr(tbox_address, vbLF) > 0
MsgBox "vbCrLf " & Instr(tbox_address, vbCrLF) > 0
End Sub

Now you understand why it is not good to use a single control for multiple Data Fields

10-07-2015, 01:36 AM
Use the Builtin tools in Word.

Put a docvariable field with the name 'address' in the word document where you want the address to appear

In Excel

Sub M_snb()
with getobject("C:\template.doc")
.variables("address")=join(array("Name","street","State","zip"), vbcr)
.printout false
.close 0
end with
End Sub