0

I am looking to create a rule that will conditionally add a BCC recipient depending on the domain name of the To/CC recipients. I have already identified this question as something similar but it doesn't seem to have been resolved.

The starting code is as below:

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 = "SomeEmailAddress@domain.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

In pseudo-code I am looking to add the following conditionals to the strBCC string:

If ToCCRecipientDomain = "@example1.co.uk"
  Then strBCC = "email1@trello.com"
ElseIf ToCCRecipientDomain ="@example2.co.uk"
  Then strBCC = "email2@trello.com"
ElseIf ToCCRecipientDomain ="@example3.co.uk"
  Then strBCC = "email3@trello.com"
Else
  Then Cancel = True
End If

For those interested in the application/reason for this, I am looking to create a list of emails sent to a client on a Trello Board for the particular project, which will depend on the email address being sent to.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

0

I think I am close with this as below, simply adding additional If & EndIf lines when additional conditions are required.

If InStr(Item.Recipients.Address, "@example1.co.uk") Then
    strBcc = "email1@trello.com"
If InStr(Item.Recipients.Address, "@example2.co.uk") Then
    strBcc = "email2@trello.com"
Else
    Cancel = True
End If
End If
  • I think the problem is in the "Item.Recipients.Address" - I don't think this is calling up the address of the email recipients – Richard Allan Jun 03 '16 at 09:53