VBA outlook - 从excel检索电子邮件地址作为收件人

时间:2015-01-22 14:02:02

标签: excel vba outlook-vba

我想从excel单元格中检索电子邮件地址,并将其作为Outlook上的收件人进行复制。

然而," To"和" CC"前景空虚。

输入和输出:

单元格A1是我想"发送到" 的电子邮件地址。

单元格A2是我想要" CC到" 的电子邮件地址。

我的VBA代码:

Sub Button1_Click()

    Dim OutApp As Object
    Dim OutMail As Object




    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(olMailItem)



    On Error Resume Next
    With OutMail
        .To = Cells("A1")
        .CC = Cells("A2")
        .BCC = ""
        .Subject = "This is the Subject line"

    End With
    On Error GoTo 0


    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

3 个答案:

答案 0 :(得分:1)

如果您删除" On Error Resume Next"你可以调试。以下内容无效:

.To = Cells("A1")
.CC = Cells("A2")

尝试

.To = Range("A1")
.CC = Range("A2")

答案 1 :(得分:0)

我有一个Recipient的好运:

'If not defined:
'olBCC=3
'olCC=2
'olTo=1

Set OutMail = Application.CreateItem(olMailItem) 
Set myRecipient = OutMail.Recipients.Add(Range("A1"))
'myRecipient.Type = olTo
'This is default - use for clarity if desired
Set myRecipient = OutMail.Recipients.Add(Range("A2")) 
myRecipient.Type = olCC

如果您想添加多个收件人,则必须一次添加一个

答案 2 :(得分:0)

您需要添加收件人,而不是To,CC或BCC属性。这些属性仅包含显示名称。应使用Recipients集合来修改此属性。例如:

Sub CreateStatusReportToBoss()  
   Dim myItem As Outlook.MailItem  
   Dim myRecipient As Outlook.Recipient 
   Set myItem = Application.CreateItem(olMailItem)  
   Set myRecipient = myItem.Recipients.Add("Dan Wilson")  
   myItem.Subject = "Status Report"  
   myItem.Display  
End Sub

您可能会发现以下文章有用: