Consulting

Results 1 to 3 of 3

Thread: userform text box formatting and exporting to word template

  1. #1
    VBAX Regular
    Joined
    Feb 2015
    Posts
    81
    Location

    userform text box formatting and exporting to word template

    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
    87598

    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.Activate
        
        wrdApp.Selection.Find.ClearFormatting
        wrdApp.Selection.Find.Replacement.ClearFormatting
        
        wrdNNDF.doc_RR_tbox_address.Text = TESTMASTER_optbut.tbox_address.Text
    
    
        wrdNNDF.PrintOut
        wrdNNDF.Close SaveChanges:=wdDoNotSaveChanges
        wrdApp.Quit
        Unload Me
    End Sub

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    It's easier to use
    tbxName
    tbxAdd1
    tbxAdd2
    tbxCity
    tbxState
    tbxZip
    In both Excel and Word UserForms and documents

    With wrdNNDF
    .doc_RR_tbox_Name = Me.tbxName
    .doc_RR_tbox_Add1 = Me.tbxAdd1
    'Etc
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    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)
         .fields.update
         .printout false
         .close 0
      end with
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •