Consulting

Results 1 to 6 of 6

Thread: Solved: Copying text from 7 textboxes via cmdButton

  1. #1
    VBAX Regular
    Joined
    Nov 2008
    Posts
    14
    Location

    Solved: Copying text from 7 textboxes via cmdButton

    I have the vba code to which enables me to copy text from several textboxes to the clipboard. The textboxes (1-7) are:

    Company Name (TextBox 1)
    Add Line 1
    Add Line 2
    Add Line 3
    City
    County
    Post Code (TextBox 7)

    The button works fine and copies the text from the textboxes to the clipboard so that i can use it in other programs eg. word.

    I have now discovered that there isnt always a Address Line 3 and sometimes this box can be left blank.

    Is there any code available to say if any of the textboxes (1-7) is blank or doesn't contain anything/any text, then not to include it in the copy function? So that when i paste it into word it only pastes the textboxes which have been filled in.

    Many thanks, Matt

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings Matt,

    When you send it to Word, how is this being done? As one concentated string w/some linefeeds in it?

    Maybe a code snippet to show how its currently working?

    Mark

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Well, I don't know why it didn't strike me that it looks like envelope addressing the first time.

    If so, or similar, here's a simple approach.

    Hope this helps,

    Mark

    [vba]Option Explicit
    Dim strAddress As String

    Private Sub CommandButton1_Click()
    Dim dObj As DataObject
    With UserForm1
    If Not .TextBox1 = Empty Then strAddress = strAddress & .TextBox1
    If Not .TextBox2 = Empty Then strAddress = strAddress & vbCrLf & .TextBox2
    If Not .TextBox3 = Empty Then strAddress = strAddress & vbCrLf & .TextBox3
    If Not .TextBox4 = Empty Then strAddress = strAddress & vbCrLf & .TextBox4
    If Not .TextBox5 = Empty Then strAddress = strAddress & vbCrLf & .TextBox5
    If Not .TextBox6 = Empty Then strAddress = strAddress & vbCrLf & .TextBox6
    If Not .TextBox7 = Empty Then strAddress = strAddress & vbCrLf & .TextBox7
    End With

    Set dObj = New DataObject

    dObj.SetText strAddress, 1
    dObj.PutInClipboard

    End Sub[/vba]

  4. #4
    VBAX Regular
    Joined
    Nov 2008
    Posts
    14
    Location

    Smile Thank you

    Excellent, thanks GTO works a treat!

    Many thanks.

  5. #5
    VBAX Regular
    Joined
    Feb 2015
    Posts
    9
    Location
    I know this is old, but what was the entire code after it was completed?

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the forum.

    open a new thread and post your question to that thread please.
    uploading your workbook (sensitive data replaced/removed) will help members help you.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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