Word 2013- VBA-如何从用户表单列表框中向结果添加标点符号? [英] Word 2013- VBA- How To Add Punctuation to Results From A User Form List Box?
本文介绍了Word 2013- VBA-如何从用户表单列表框中向结果添加标点符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有四个条目的列表框(多选)的用户表单.
I have a user form with list box (multi selection) with four entries.
以下代码构建字符串:
Dim myArray() As String
'Use Split function to return a zero based one dimensional array.
myArray = Split("text 1|" _
& "text 2|" _
& "text 3|" _
& "text 4", "|")
'Use .List method to populate listbox.
ListBox1.List = myArray
'Use the .ColumnWidth property to set column widths. 0 results in a hidden column.
ListBox1.ColumnWidths = "1"
lbl_Exit:
Exit Sub
下面的以下代码成功插入了四个选定条目的任意组合,每个条目之间带有,
和空格,每个空格之间都包含一个.
,即最后一个 text1,.
或文本1,文本2,文本3,.
或文本1,文本2,文本3,文本4,.
进入内容控件
The following code below successfully inserts any combination of the four selected entries with a ,
and space in between each entry including a .
at the end i.e. text 1, .
or text 1, text 2, text 3, .
or text 1, text 2, text 3, text 4, .
into a content control.
Dim SelectedTexts As String
Dim index As Long
For index = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(index) Then
'Adds comma after every entry
SelectedTexts = SelectedTexts & ListBox1.List(index) & ", "
End If
Next
'Adds period to the end
ActiveDocument.SelectContentControlsByTitle("test").Item(1).Range.Text = Mid(SelectedTexts, 1) _
& "."
Unload Me
lbl_Exit:
Exit Sub
Me.Repaint
Me.Hide
我正在尝试完成两件事:
I am trying to accomplish two things:
- 我只想要最后一个条目之后的
.
,即text 1.
NOTtext 1,.
- 我希望在最后一个结果之前有一个
和
,即文本1和文本2.
或文本1,文本2和文本3.
或文本1,文本2和文本4.
- I only want a
.
after the last entry i.e.text 1.
NOTtext 1, .
- I want there to be an
and
before the last result i.e.text 1 and text 2.
ortext 1, text 2 and text 3.
ortext 1, text 2 and text 4.
推荐答案
一旦构建了字符串,就需要将其处理为所需的形式.这是执行此操作的逻辑:
Once your string is built, you need to manipulate it into the form you require. Here is the logic to do this:
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
这篇关于Word 2013- VBA-如何从用户表单列表框中向结果添加标点符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文