Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 05-05-2019, 12:21 AM
subrota subrota is offline Windows 10 Office 2013
Novice
 
Join Date: Apr 2019
Posts: 8
subrota is on a distinguished road
Default Copy all items from listbox and paste to word document

I have a listbox with items . Now I want to copy the items as a list and paste to the word document. I can not select the listbox all item as string.

Code:
Dim strClipText As DataObject
'Dim strInputText As String
'Set strClipText = New DataObject
'
'strInputText = listbox1.text
'
'strClipText.SetText strInputText
'strClipText.PutInClipboard

Reply With Quote
  #2  
Old 05-05-2019, 12:36 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,392
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

If you want to output all the list entries to the document, simply loop through them, adding each to a string variable, then write that variable's content to the document. There is no need to involve the clipboard.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 05-05-2019, 08:41 AM
gmaxey gmaxey is offline Windows 10 Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 843
gmaxey will become famous soon enough
Default

Code:
Private Sub CommandButton1_Click()
Dim bListAll As Boolean
Dim strList As String
Dim lngIndex As Long
  'Do you want to list "all" items in the listbox?
  bListAll = True
  'or do you want to list the item or items selected? Then set the variable above to False
  If bListAll Then
    For lngIndex = 0 To ListBox1.ListCount - 1
      If strList = vbNullString Then
        strList = ListBox1.List(lngIndex)
      Else
        strList = strList & vbCr & ListBox1.List(lngIndex)
      End If
    Next lngIndex
  Else
    For lngIndex = 0 To ListBox1.ListCount - 1
      If ListBox1.Selected(lngIndex) Then
        If strList = vbNullString Then
          strList = ListBox1.List(lngIndex)
        Else
          strList = strList & vbCr & ListBox1.List(lngIndex)
        End If
      End If
    Next lngIndex
  End If
  ActiveDocument.Range.Text = strList
  Hide
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 05-06-2019, 04:07 AM
subrota subrota is offline Windows 10 Office 2013
Novice
 
Join Date: Apr 2019
Posts: 8
subrota is on a distinguished road
Default

Paul Edstein
Thanks for your instruction. I did try and try and at last I got the code as your instruction.

Greg Maxey: According Paul Edstein's instruction and your code make me easy to understand me. Hope I will go forward with your helping hand.

Sometimes some easy thing is going complex. I hope it will be more easier day by day. Wish for me.

Sorry for my poor English


Thanks and Thanks.
Subrota



Code:
Dim mySym As String
Dim i As Long
Dim strClipText As DataObject
Dim strInputText As String

For i = 0 To ListBox2.ListCount - 1
mySym = mySym & ListBox2.List(i)
mySym = mySym & vbCrLf
Next


Set strClipText = New DataObject
strInputText = mySym

strClipText.SetText strInputText
strClipText.PutInClipboard
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy and paste Outlook Email bodytext to Word document eYos Word VBA 1 08-25-2015 09:41 PM
How can I let my users copy and paste into a protected Word Document? moden9999 Word 4 02-02-2015 05:39 PM
Moving Selected Items from a Multiselect Listbox on a userform to a bookmark in Word marksm33 Word VBA 3 01-15-2015 07:55 PM
Getting mult resps. selected in listbox to concatenate & paste at a bookmark in Word marksm33 Word VBA 3 01-15-2015 05:59 PM
copy from web and paste in a word document : no images are shown Ron Wolpa Word 5 09-11-2013 02:16 AM


All times are GMT -7. The time now is 11:00 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft