Wednesday, July 15, 2009

How to automatically BCC all outgoing messages in Outlook

From http://www.outlookcode.com/article.aspx?id=72 we read:

Outlook has a rule to automatically Cc another person on outgoing messages, but no equivalent for Bcc. This page offers two code samples for adding such an automatic Bcc. Both use the Application.ItemSend event, which fires whenever a user sends a message or other item.

Method #1 (Basic)

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:

Private Sub Application_ItemSend(ByVal Item As Object, _
Cancel As Boolean)
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 = "someone@somewhere.dom"

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

Make sure you substitute the right e-mail address for "someone@somewhere.dom."

The reason that this method is not suitable for versions earlier than Outlook 2003 is because it will trigger an address book security prompt due to the use of Recipients.Add. You could avoid security prompts by simply setting the Item.Bcc property to the desired address, but that has two problems. First, it would wipe out any Bcc recipients that the user might have already added. Also, in some Outlook configurations, setting Bcc without trying to resolve the address results in an unresolved address, even if you use a proper SMTP address; you'll get an error, and Outlook won't send the message.

Method #2 (Redemption)

This version uses the same basic technique as Method #1, only with the third-party Outlook Redemption library to avoid security prompts in versions before Outlook 2003 and, if the recipient cannot be resolved, to display to the user the name resolution dialog.

Private Sub Application_ItemSend(ByVal Item As Object, _
Cancel As Boolean)
' Requires a reference to
' the SafeOutlook library (Redemption.dll)
Dim objMe As Redemption.SafeRecipient
Dim sMail As Redemption.SafeMailItem
On Error Resume Next

Set sMail = CreateObject("Redemption.SafeMailItem")
Item.Save
sMail.Item = Item
Set objMe = sMail.Recipients.Add("myaddress@mydomain.dom")
objMe.Type = olBCC
If Not objMe.Resolve(True) Then
Cancel = True
End If

Set objMe = Nothing
Set sMail = Nothing
End Sub

Make sure you substitute the right e-mail address for "myaddress@mydomain.dom."

No comments:

Post a Comment