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

如何将用户表单列表框中的选择填充到MS Word VBA中?,vba,drop-down-menu,ms-word,Vba,Drop Down Menu,Ms Word,编辑 参考我之前提出的一个问题,我将如何更改以下代码以项目符号格式而不是逗号填充结果 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 & ListB

编辑

参考我之前提出的一个问题,我将如何更改以下代码以项目符号格式而不是逗号填充结果

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

这是一项微不足道的工作。有人怀疑您是否试图修改现有代码。如果对现有的内容控件应用合适的项目符号格式,那么整个过程都可以用它来完成。例如:

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

此外,当我插入更多下拉列表并进行选择时,它应将该项目从所有以前的现有列表中删除,以防止重复选择根据您从以后插入的列表中删除已选项目的计划,这不是一种可能的方案。您可以通过显示代码和解释已尝试的内容来改进您的问题。格雷格·马克西的网页有用吗?如果没有,您会遇到什么问题?另请参见:@Charles Kenyon和@Tim Williams-感谢您的反馈。请看我编辑的问题这是一个与原始问题完全不同的问题,不是编辑问题。今后请提出一个新问题,而不是完全改变已有答案的现有问题。@Timothy Rylatt-是的,先生。链接到正确的问题:Per@Timothy Rylatt,链接到正确的问题:你为什么问我已经回答过的同一个问题?先生,我在遵循上面的Timothy Rylatt的指导