如何在 Excel VBA 中访问联系人组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10049419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access contact groups in Excel VBA?
提问by Moses
I am building an Excel add-in that sends the active workbook as an attachment in an Outlook email template to a specific Contact Group.
我正在构建一个 Excel 加载项,它将活动工作簿作为 Outlook 电子邮件模板中的附件发送到特定的联系人组。
I've gotten the first two parts to work with the code below, but I am not sure how to set the .TO
field to a contact group.
我已经得到了前两部分来处理下面的代码,但我不确定如何将该.TO
字段设置为联系人组。
Public Sub Mail_Reports()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
On Error Resume Next
Set OutApp = CreateObject("Outlook.Application")
'Set this line to the path and file name of your template
Set OutMail = OutApp.CreateItemFromTemplate("C:\Users\moses\AppData\Roaming\Microsoft\Templates\test.oft")
On Error Resume Next
With OutMail
'.TO field should be set to the contact group
.BCC = ""
.Attachments.Add ActiveWorkbook.FullName
.HTMLBody = Replace(OutMail.HTMLBody, strOldPeriod, strNewPeriod)
.Subject = Replace(OutMail.Subject, strOldPeriod, strNewPeriod)
'To display the email leave as is; to send the Email, change to .Send
.Display 'or Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
采纳答案by Doug Glancy
Just use the name of the contact group (formerly called "distribution lists"). I just tried it, as suggested on Ron de Bruin'ssite, and it works.
只需使用联系人组的名称(以前称为“通讯组列表”)。我刚刚按照Ron de Bruin网站上的建议进行了尝试,并且有效。
回答by Drew Chapin
In order to have the recipient's email addresses or names resolved (so they don't display just plain text), you can do the following.
为了解析收件人的电子邮件地址或姓名(因此它们不只显示纯文本),您可以执行以下操作。
With OutMail
'.TO field should be set to the contact group
.BCC = ""
.Attachments.Add ActiveWorkbook.FullName
.HTMLBody = Replace(OutMail.HTMLBody, strOldPeriod, strNewPeriod)
.Subject = Replace(OutMail.Subject, strOldPeriod, strNewPeriod)
'To display the email leave as is; to send the Email, change to .Send
.Display 'or Send
If Not .Recipients.ResolveAll Then
For Each Recipient In .Recipients
If Not Recipient.Resolved Then
MsgBox Recipient.Name & " could not be resolved"
End If
Next
End If
End With