Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using Outlook VBA to Automatically BCC Emails

Posted on 2011-09-21
3
Medium Priority
?
725 Views
Last Modified: 2012-05-12
I am trying to use a variant of this widely available VBA code to automatically BCC  all email sent from Outlook 2007 to 2 email address.  

 Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'comments
'This version is suitable for Outlook 2003 or later. It uses Outlook objects exclusively and
'includes error handling to avoid problems with an invalid Bcc address.
'Place this VBA code in the built-in ThisOutlookSession module
 

    Dim objRecip As Recipient
    Dim strMsg As String
    Dim res As Integer
    Dim strBcc As String
    On Error Resume Next
 
    ' #### USER OPTIONS ####
    ' address for Bcc -- must be SMTP address or resolvable
    ' to a name in the address book
    strBcc = "email1@somedomain.com"
    Set objRecip = Item.Recipients.Add(strBcc)
    objRecip.Type = olBCC
    
    strBcc = "email2@somedomain.com"
    Set objRecip = Item.Recipients.Add(strBcc)
    objRecip.Type = olBCC
         
      
    If Not objRecip.Resolve Then
        strMsg = "Could not resolve the Bcc recipient. " & _
                 "Do you want still to send the message?"
        res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
                "Could Not Resolve Bcc Recipient")
        If res = vbNo Then
            Cancel = True
        End If
    End If
 
    Set objRecip = Nothing
End Sub

Open in new window


When I send the email I immediately receive the following report into my inbox

Your message did not reach some or all of the intended recipients.

      Subject:      Subject Here
      Sent:      21/09/2011 10:23

The following recipient(s) cannot be reached:


Yet the email addresses are all valid and the emails are being delivered,  and the report lists no email addresses.

How do I prevent this report being triggered, why am I receiving it.

Is it a bug in my code or something else? When I only use one email address for BCC it works perfectly.
 When the BCC field is filled in manually, everything works properly and I get no errors or reports.  This suggests to me that there may be a problem with the code.
0
Comment
Question by:weegiraffe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 2000 total points
ID: 36590807
Hi, weegiraffe.

I don't see any errors in the code.  That said, the code is only resolving the second BCC address.  The fact that the first address isn't resolved could be causing the problem.  I've modified the code to include modifying the way it words so all of the BCC addresses are resolved, if any of them are unresolved the user is offered the chance to cancel the message, and you can now add more BCC addressees without having to write more code.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    'On the next line edit the list of address to BCC to'
    Const BCC_RECIPIENTS = "email1@somedomain.com,email2@somedomain.com"
    Dim objRecip As Recipient, arrRecipients As Variant, varRecipient As Variant
    On Error Resume Next
    arrRecipients = Split(BCC_RECIPIENTS, ",")
    For Each varRecipient In arrRecipients
        Set objRecip = Item.Recipients.Add(varRecipient)
        objRecip.Type = olBCC
        If Not objRecip.Resolve Then
            If MsgBox("Could not resolve the Bcc recipient. Do you want still to send the message?", vbYesNo + vbDefaultButton1, "Could Not Resolve Bcc Recipient") = vbNo Then
                Cancel = True
                Exit For
            End If
        End If
    Next
    On Error GoTo 0
    Set objRecip = Nothing
End Sub

Open in new window

0
 

Author Comment

by:weegiraffe
ID: 36713115
Thank you BlueDevilFan.  That code works.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 36714125
You're welcome.  Glad I could help.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
The core idea of this article is to make you acquainted with the best way in which you can export Exchange mailbox to PST format.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question