使用“抄送:”字段和/或“密件抄送:”字段没有“收件人:”字段,将电子邮件地址放入“收件人:”字段

时间:2018-11-05 20:45:21

标签: outlook-vba

从VBA向Outlook发送电子邮件时遇到问题。当我将电子邮件地址分配给“收件人:”,“抄送”和“密件抄送:”字段时,根据字段分配的顺序,它们没有被放入电子邮件的正确框中。我必须最后分配To:字段,否则它将cc或bcc放在To:字段中。

按此顺序的代码(收件人,密件抄送,抄送)-

'These have to be 3, 2, 1 or else the BCC: or CC: shows up in the To: field of the email
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(RecipientList)
objOutlookRecip.Type = 1  ' 1 = olTo  
'Add those who are being bcc'd on this email
Set objOutlookRecip = .Recipients.Add(bccList)
objOutlookRecip.Type = 3  ' 3 = olBCC 
'Add those who are being cc'd on this email
Set objOutlookRecip = .Recipients.Add(ccList)
objOutlookRecip.Type = 2  ' 2 = olCC 

电子邮件看起来像这样:

To: = cc@cc.com
CC: = ""
BCC: = bcc@cc.com

按此代码顺序(收件人,抄送,密件抄送)

'These have to be 3, 2, 1 or else the BCC: or CC: shows up in the To: field of the email
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(RecipientList)
objOutlookRecip.Type = 1  ' 1 = olTo 
'Add those who are being cc'd on this email
Set objOutlookRecip = .Recipients.Add(ccList)
objOutlookRecip.Type = 2  ' 2 = olCC  
'Add those who are being bcc'd on this email
Set objOutlookRecip = .Recipients.Add(bccList)
objOutlookRecip.Type = 3  ' 3 = olBCC  

电子邮件看起来像这样:

To: = bcc@cc.com
CC: = cc@cc.com
BCC: = ""

代码按此顺序排列(密件抄送,抄送,收件人)

'These have to be 3, 2, 1 or else the BCC: or CC: shows up in the To: field of the email
'Add those who are being bcc'd on this email
Set objOutlookRecip = .Recipients.Add(bccList)
objOutlookRecip.Type = 3  ' 3 = olBCC 
'Add those who are being cc'd on this email
Set objOutlookRecip = .Recipients.Add(ccList)
objOutlookRecip.Type = 2  ' 2 = olCC 
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(RecipientList)
objOutlookRecip.Type = 1  ' 1 = olTo 

像这样正确发送电子邮件:

To: = ""
CC: = cc@cc.com
BCC: = bcc@cc.com

此外,如果我要发送带有cc:和/或bcc:且没有To:的电子邮件,则会收到“错误440:“收件人”,“抄送”或“密件抄送”框中必须至少有一个名称或联系人组”。它说“收件人,抄送为 OR 密件抄送”,我还有另外两个,为什么我会收到此错误?

我在避免错误的其他东西之前添加了这段代码:

If Len(RecipientList) = 0 Then
    RecipientList = " "
End If

这是调用例程:

SendTestHTMLMessages "to@to.com", "cc@cc.com:", "bcc@bcc.com", "Test Message Subject", "Test Message Body"

这是工作代码:

Sub SendTestHTMLMessages(RecipientList As String, Optional ccList As String, Optional bccList As String, Optional Subject As String, Optional Body As String)

   Dim objOutlook As Object ' Outlook.Application
   Dim objOutlookMsg As Object ' Outlook.MailItem
   Dim objOutlookRecip As Object ' Outlook.Recipient

   Dim Signature As String

   ' Create the Outlook session.
   Set objOutlook = CreateObject("Outlook.Application")
   objOutlook.Session.Logon

   ' Create the message.
   Set objOutlookMsg = objOutlook.CreateItem(0)   '0 = olMailItem  (Late Binding)

If Len(RecipientList) = 0 Then
    RecipientList = " "
End If

With objOutlookMsg

    'These have to be 3, 2, 1 or else the BCC: or CC: shows up in the To: field of the email
    If Len(bccList) > 0 Then
        'Add those who are being bcc'd on this email
        Set objOutlookRecip = .Recipients.Add(bccList)
        objOutlookRecip.Type = 3  ' 3 = olBCC
    End If
    If Len(ccList) > 0 Then
        'Add those who are being cc'd on this email
        Set objOutlookRecip = .Recipients.Add(ccList)
        objOutlookRecip.Type = 2  ' 2 = olCC
    End If
    If Len(RecipientList) > 0 Then
        ' Add the To recipient(s) to the message.
        Set objOutlookRecip = .Recipients.Add(RecipientList)
        objOutlookRecip.Type = 1  ' 1 = olTo
    End If

     ' Set the Subject & Body of the message.
     .Subject = Subject
     .htmlBody = Body
     '.BodyFormat = 3   '3 = olFormatRichText
    Set .SendUsingAccount = objOutlook.Session.Accounts.Item(1)

       .Display

   End With


End Sub 

我已经完成了这项工作,但是感觉很不灵活,我想了解为什么它会这样工作。我希望有一些见识。

提前谢谢! 金

1 个答案:

答案 0 :(得分:0)

objOutlookRecip.Type =之后跟随objOutlookRecip.Resolve

或者是objOutlookMsg.Recipients.ResolveAll之后的End With