且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将用户表单列表框中的选择填充到项目符号MS-Word-VBA中?

更新时间:2023-10-05 18:53:22

这是一项微不足道的任务.一个人想知道您是否甚至尝试过修改现有代码.如果对它应用合适的项目符号格式,则可以使用现有的内容控件完成全部操作.例如:

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