简体   繁体   中英

How To Populate Selections From A User Form List Box into Bullets MS-Word-VBA?

EDITED

Referring to a question I asked previously Word 2013- VBA- How To Add Punctuation to Results From A User Form List Box? , how would I change the following code to populate the results in a bullet format instead of commas?

Private Sub Test()
   Dim SelectedTexts As String
   Dim index As Long
   
   For index = 0 To ListBox1.ListCount - 1
      If ListBox1.Selected(index) Then
         SelectedTexts = SelectedTexts & ListBox1.List(index) & ", "
      End If
   Next

   SelectedTexts = Mid(SelectedTexts, 1, Len(SelectedTexts) - 2) & "."
   index = InStrRev(SelectedTexts, ",")
   If index > 0 Then SelectedTexts = Left(SelectedTexts, index - 1) & " and " & Right(SelectedTexts, Len(SelectedTexts) - index - 1)

   ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = SelectedTexts
End Sub

This is a trivial undertaking. One wonders whether you even tried to modify your existing code. The whole lot could be done with your existing content control if you apply a suitable bullet format to it. For example:

Private Sub Test()
   Dim SelectedTexts As String
   Dim index As Long
   
   For index = 0 To ListBox1.ListCount - 1
      If ListBox1.Selected(index) Then
         SelectedTexts = SelectedTexts & ListBox1.List(index) & ";" & vbCr
      End If
   Next

   SelectedTexts = Mid(SelectedTexts, 1, Len(SelectedTexts) - 2) & "."
   index = InStrRev(SelectedTexts, vbCr)
   If index > 0 Then SelectedTexts = Left(SelectedTexts, index - 1) & " and " & Right(SelectedTexts, Len(SelectedTexts) - index + 1)

   ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = SelectedTexts
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM