我设置了一个发送电子邮件的用户表单。有一个列表框,其中包含映射到.To属性的电子邮件地址列表。我希望显示人员的姓名,然后映射到他们的电子邮件地址,而不是在列表框中包含电子邮件地址。我怎么能这样做?
这是填充列表框的代码。
Private Sub UserForm_Initialize()
With ToBox
.AddItem "john@business.com"
.AddItem "bill@business.com"
.AddItem "mary@business.com"
.AddItem "adam@business.com"
.AddItem "jane@business.com"
End With
End Sub
答案 0 :(得分:2)
这是你在尝试的吗?
Dim EmailArray(1 To 15, 1 To 15) As String
Dim i As Long
Private Sub UserForm_Initialize()
'~~> Set your 15 names and email address here
EmailArray(1, 1) = "Siddharth Rout": EmailArray(1, 2) = "Blah@Siddharthrout.Com"
EmailArray(2, 1) = "Name1 Here": EmailArray(2, 2) = "Email1 Here"
EmailArray(3, 1) = "Name2 Here": EmailArray(3, 2) = "Email2 Here"
'
' And so on..
'
EmailArray(15, 1) = "Name15 Here": EmailArray(15, 2) = "Email15 Here"
'~~> Add the names from Col 1 to listbox
For i = 1 To 15
ToBox.AddItem EmailArray(i, 1)
Next i
End Sub
'~~> Match the value from listbox with array and retrieve the
'~~> corresponding column value
Private Sub CommandButton1_Click()
If ToBox.ListIndex <> -1 Then
For i = 1 To 15
If ToBox.Value = EmailArray(i, 1) Then
MsgBox "the name you selected is " & EmailArray(i, 1) & _
" and the email address is " & EmailArray(i, 2)
Exit For
End If
Next i
End If
End Sub
答案 1 :(得分:0)
这不是最优雅的解决方案,但我想出了这个,以此来做我想做的事。在实际发送电子邮件的Sub中,我做到了这一点。它接受ComboBox的值并通过If例程来获取电子邮件地址。
If Questions.ToBox.Value = "John" Then
.To = "john@business.com"
End If
If Questions.ToBox.Value = "Bill" Then
.To = "bill@business.com"
End If
If Questions.ToBox.Value = "Mary" Then
.To = "mary@business.com"
End If