从列表框值创建具有多个收件人的电子邮件 [英] Create email with multiple recipients from listbox values
问题描述
我正在尝试创建电子邮件,并基于列表框填充多个收件人.
I am trying to create an email and populate multiple recipients based off a listbox.
我尝试将列表框列引用放在".To"行中,但它给出了空错误.
I tried putting the list box column reference in the ".To" line but it gives a null error.
我发现应该循环遍历列表框值的代码,但是没有填充任何收件人.
I found code that should loop through the listbox values but it is not populating any recipients.
Public Sub cmdEmailContact_Click()
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Dim strPath As String
Dim strFilter As String
Dim strFile As String
Dim strFileEnd As String
Dim strEmailRecipients As String
strPath = "C:\Users\username\Desktop\Invoice Test\GCX"
strFilter = Me.txtInvNum
strFileEnd = ".pdf"
strFile = Dir(strPath & strFilter & strFileEnd)
strEmailRecipients = ""
For N = 0 To Me.lstContacts.ListCount - 1
If Me.lstContacts.Selected(N) = True Then
strEmailRecipients = strEmailRecipients & "; " & Me.lstContacts.Column(3, N)
End If
Next N
strEmailRecipients = Mid(strEmailRecipients, 3)
If strFile <> "" Then
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatRichText
.To = strEmailRecipients
''.cc = ""
''.bcc = ""
.Subject = "text here"
.SentOnBehalfOfName = "emailname"
.HTMLBody = "text here"
.Attachments.Add (strPath & strFilter & strFileEnd)
'.Send
.Display
End With
Else
MsgBox "No file matching " & strPath & strFilter & strFileEnd & " found." & vbCrLf & _
"Process has been stopped."
Exit Sub
End If
End Sub
我希望strEmailRecipients等于分号分隔的基于列表框的电子邮件地址列表.没有错误消息.
I expect strEmailRecipients to equal a semi-colon separated list of email addresses based off the listbox. There are no error messages.
推荐答案
而不是构建用分号分隔的字符串来填充 MailItem
对象,您可能想修改 MailItem
对象.
Rather than building a semi-colon delimited string to populate the To
property of the MailItem
object, you may instead want to modify the contents of the Recipients
collection when adding recipients (independent of the recipient type) to a MailItem
object.
将项目添加到 Recipients
使用 Add
方法进行收集将产生一个 Recipient
对象,该对象具有 Type
属性通过将属性设置为 olTo
,
Adding an item to the Recipients
collection using the Add
method will yield a Recipient
object, which has a Type
property which may be used to designate the recipient as either to, cc, or bcc by setting the property to olTo
, olCC
, or olBCC
(or 1
, 2
, or 3
if using late binding).
因此,电子邮件的结构可能类似于以下内容:
Hence the construction of the email might become something along the lines of the following:
Dim idx
With MailOutLook
With .Recipients
For Each idx In lstContacts.ItemsSelected
With .Add(lstContacts.ItemData(idx))
.Type = olTo
End With
Next idx
End With
.BodyFormat = olFormatRichText
' ... etc.
End With
这篇关于从列表框值创建具有多个收件人的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!