Auto BCC VBA macro: how to add exceptions?

Status
Not open for further replies.

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
it would go at the top of the macro. if you need to do this for a multiple names, i would use categories or a keyword in the subject.
if item.categories = "no bcc" then

exit sub

else

'bcc code

end if
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
everything before the Private is the problem. the code goes in the macro, not before.

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

if item.categories = "no bcc" then
exit sub
else

' #### USER OPTIONS ####
' address for Bcc -- must be SMTP address
' or resolvable to a name in the address book
strBcc = "address@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 to send the message?"
res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
"Could Not Resolve Bcc")
If res = vbNo Then
Cancel = True
End If
End If



end if

Set objRecip = Nothing

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
You'd probably want to check for a category, either using if item.categories <> "bcc" then

or run the code if the category exists

if item.categories = "bcc" then

' bcc code

else

end if
 

boppy2010

Senior Member
Unfortunately this is my first exposure to VBA plus I have a cold so my head is not working right. I've moved that text but still getting an error, albeit it in a different place:

http://imm.io/19xQ9

I've pasted the code here in the hopes that you may take pity on my befuddled state and put it in the right order ...

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 = "1kawa9@mails.zohocrm.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

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

If Item.Categories = "personal" Then

Exit Sub

Else

'bcc code

end if Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

' #### USER OPTIONS ####

' address for Bcc -- must be SMTP address or resolvable

' to a name in the address book

strBcc = "1kawa9@mails.zohocrm.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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I think its still the red line - in the sample above, you have

end if Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

it should be

end if

end sub

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
It looks like you have 2 or 3 different copies of the macro or bits of the macro. You only need one macro, if you want to use different bcc addresses, you need to use if statements. This sample bcc's different addresses based on the category you assigned to the item.

Code:
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
 
   If Item.Categories = "Personal" Then
 
   ' #### USER OPTIONS ####
   ' address for Bcc -- must be SMTP address
   ' or resolvable to a name in the address book
     strBcc = "address@domain.com" 
 
ElseIf Item.Categories = "Important" Then
     strBcc = "new@address.com"
  
    Else
     Exit Sub
   End If
 
   Set objRecip = Item.Recipients.Add(strBcc)
   objRecip.Type = olBCC
   If Not objRecip.Resolve Then
       strMsg = "Could not resolve the Bcc recipient. " & _
                "Do you want to send the message?"
       res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
               "Could Not Resolve Bcc")
       If res = vbNo Then
           Cancel = True
       End If
   End If
   Set objRecip = Nothing 
 
End Sub
 

boppy2010

Senior Member
So in this section:
strBcc = "address@domain.com"
ElseIf Item.Categories = "Important" Then
strBcc = "new@address.com"

> .. I presume the first address@domain is my Zoho CRM address but what goes in "new@address.com"?

But then it doesn't seem to be using the "personal" category to add exceptions to the bcc.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
the second address is if you want to BCC to other addresses. if you only use one bcc, use
If Item.Categories = "Personal" Then

' #### USER OPTIONS ####
' address for Bcc -- must be SMTP address
' or resolvable to a name in the address book
strBcc = "address@domain.com"
Else
Exit Sub
End If

If you want to BCC everything except items marked personal (or whatever category you decide),

use If Item.Categories <> "Personal" Then

and add a category when you don't want to BCC a message.
 

boppy2010

Senior Member
Well this is what I have now and it's not bbc-ing:
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

If Item.Categories = "Personal" Then

' #### USER OPTIONS ####
' address for Bcc -- must be SMTP address
' or resolvable to a name in the address book
strBcc = "1kawa9@mails.zohocrm.com"

Else
Exit Sub
End If

Set objRecip = Item.Recipients.Add(strBcc)
objRecip.Type = olBCC
If Not objRecip.Resolve Then
strMsg = "Could not resolve the Bcc recipient. " & _
"Do you want to send the message?"
res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
"Could Not Resolve Bcc")
If res = vbNo Then
Cancel = True
End If
End If

Set objRecip = Nothing
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Did you set a category on the message you were sending?

Is the macro in ThisOutlookSession?

Is macro security set to low?
 

boppy2010

Senior Member
No I want to BCC ALL addresses except those marked personal. it would be too complicated to make a category for every other email I send.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Right, you want the default to the most used.

This should be

If Item.Categories <> "Personal" Then
' #### USER OPTIONS ####
' address for Bcc -- must be SMTP address
' or resolvable to a name in the address book
strBcc = "1kawa9@mails.zohocrm.com"
Else
Exit Sub
End If

That will tell it to set the bcc address and continue if the category is not personal or is not present. If its found, the macro exits.
 
Status
Not open for further replies.
Top