Outlook 2007 VBA - BCC depending on To/CC Recipients - vba

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.

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

Related

Automatically Send BCCs to Mailbox Sent From

We've got multiple users using an existing mailbox on Outlook. Everyone who sends from the mailbox recieves the "sent items" in their own personal mailbox. I've looked in rules and cannot find anything to have the sent items appear in the group mailbox's sent items instead.
I've got the following code, but cannot work out why it's not running.
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
strBcc = "<mailboxname>"
If Item.SendUsingAccount = "<mailboxname>" Then
Set objRecip = Item.Recipients.Add(strBcc)
objRecip.Type = olBCC
'Set variable objRecip (recipient) = Item.Recipients.Add (strBcc)
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
To have the mail sent from the mailbox account you need to do the following:
With oMailItem
Set .SendUsingAccount = oOutlook.Session.Accounts.Item(iAccount)
...
End With
Where oMailItem and oOutlook refer to your relevant objects and iAccount is the index number of the mailbox you want to use. In my case I have two mailboxes available to send from, my personal one and the group one. My personal account is first (index 1) and the group mailbox is second (index 2).
Mail Items I send using this code always move to the Sent Items folder in the group mailbox rather than my personal one.

Validating Outlook Email Attachment Name through VB Macro

Im creating an outlook Macro to validate an Email attachment and recipient name before sending the mail.
The recipient name can be easily validated through the ItemSend Function on the Outlook session.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim Recipients As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim i
Dim prompt As String
Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
Set recip = Recipients.Item(i)
If InStr(LCase(recip), "bad#address.com") Then
prompt$ = "You sending this to this to " & Item.To & ". Are you sure you want to send it?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End If
Next i
End Sub
While this helps with recipients, it does not allow to validate the attachment name before sending the mail. i.e Validate the Mail Draft. The code below helps to check for attachments present on the draft but does not help validate it.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If InStr(1, Item.Body, "attach", vbTextCompare) > 0 Then
If Item.Attachments.Count = 0 Then
answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
If answer = vbNo Then Cancel = True
End If
So i tried to add item.Attachment. Name \ item.attachment.FileName but this works only if i attribute it to a outlook MailItem instead of a normal object.
Is it possible to create code to validate the attachment name for certain criteria ( name should conform to certain naming constraints ). The code has already been created and works as a normal macro and not as a session Macro.
Function Segregate_Function(Attach_Name_Pass1 As String)
Dim FullName As String
Dim Recepients As String
Region_Ext = Right(Attach_Name_Pass1, 7)
region = Left(Region_Ext, 3)
'MsgBox region
If region = "ENG" Then
Recepients = "ABC#gmail.com;XYZ#gmail.com"
Call Send_Function(Attach_Name_Pass1, Recepients)
Else
MsgBox " Not an Acceptable Attachment. Mail Could not be Generated "
End If
End Function
I would like the above code to execute when clicking on send to validate an attachment name directly, instead of having a procedural Macro running.
Do advice.
Try testing within ItemSend.
Something like this:
Option Explicit
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim att As attachment
Dim Attach_Name_Pass1 As String
Dim Region_Ext As String
Dim Region As String
Cancel = False
If Item.Attachments.count = 0 Then
If MsgBox("There's no attachment, send anyway?", vbYesNo) = vbNo Then Cancel = True
Else
Debug.Print Item.To
If InStr(Item.To, "ABC#gmail.com") > 0 Or InStr(Item.To, "XYZ#gmail.com") > 0 Then
For Each att In Item.Attachments
Attach_Name_Pass1 = att.DisplayName
Region_Ext = Right(Attach_Name_Pass1, 7)
Region = Left(Region_Ext, 3)
'MsgBox region
Debug.Print Region
If Region <> "ENG" Then
Cancel = True
MsgBox " Not an Acceptable Attachment. Send cancelled."
Exit For
End If
Next
End If
End If
End Sub

How to hide BCC field in SENT email Outlook 2010 VBA

I have a macro coded to a rule that autoforwards all incoming and sent emails to a private email address in the BCC field (any auto BCC rule is disabled at the server level.) With the help of the board here, the macro works flawlessly, and for all intents and purposes is invisible.
However, if you open the SENT message in the SENT FOLDER, the BCC field is visible to all for the world to see. I have learned this is a "feature" in Outlook, apparently since 2003.
Is there a way to suppress the visibility of the BCC field when viewing the SENT email?
Or is there a way one can set the display options of an individual folder NOT to display a BCC - EVER?
Thank you for any assistance.
My 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
Dim answer
Dim oAtt
Dim strProc1 As String
On Error GoTo Application_ItemSend_Error
strBcc = "myprivateemail#gmail.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
On Error GoTo 0
Exit Sub
Application_ItemSend_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") " & "Error on
Line " & Erl & " in procedure Application_ItemSend of VBA Document
ThisOutlookSession"
End Sub
If you want to remove BCC recipients in the Sent Items folder, listen for the Items.ItemAdd event on the Sent Items folder, loop through all recipients in the MailItem.Recipients collection and delete recipients with Recipient.Type = olBCC.
"the BCC field is visible to all for the world to see"
Well, if anyone in the world can view your own sent folder, then this is the case. Otherwise the BCC field is not part of the email, recipients do not receive it. The goal of the feature is to have the ability to recall your own BCC messages, so you do not forget that you have sent them.
Try the following...
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim olRec As Outlook.Recipient
Dim Address$
Address = "Om3r#blala.com"
Set olRec = Item.Recipients.Add(Address)
olRec.Type = olBCC
olRec.Resolve
End Sub

Outlook VBA -> Check the email extension of an address after it has been resolved

I was wondering if there is a way i can modify my Item.To in the below code block to test against the physical email address? Right now it is checking against the resolved name.
I.E. if i am testing for #here.com within John.Doe#here.com, and Outlook auto resolves the name, i'm left with a logic test of #here.com <> "John Doe". Simply does not work. Thanks for the assistance! Code follows:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'**************************************************************************************************************************
' Summary: Outlook BCC Insert util based by scan of to field for matching user
' USAGE: populate user options and insert in to the "ThisOutlookSession" code body in Outlook
'**************************************************************************************************************************
' History:
' 05/04/2015 Me Created
'**************************************************************************************************************************
Dim objRecip As Recipient
Dim strMsg As String
Dim res As Integer
Dim EmailToCheckAgainst As String
Dim BCCEmailToAdd As String
On Error Resume Next
' #### USER OPTIONS ####
EmailToCheckAgainst = "#here.com" 'email address you are checking against
BCCEmailToAdd = "BCCme#mycompany.com" 'email address you are adding as BCC
' #### END USER OPTIONS ####
If InStr(LCase(Item.To), LCase(EmailToCheckAgainst)) > 0 Then
Set objRecip = Nothing
Set objRecip = Item.Recipients.Add(BCCEmailToAdd)
objRecip.Type = olBCC
'Resolve it?
Cancel = False
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
End If
On Error GoTo 0
End Sub
Do no use the To/CC/BCC properties. Loop through all recipients in the MailItem.Recipients collection and compare the Recipient.Address property (if it is resolved) or Recipient.Name if it is not.

How to delete autoforwarded email in SENT folder Outlook 2010 Exchange

Newbie poster with Outlook VBA. Intermediate Excel VBA coder.
I have a VBA routine that autoforwards all incoming email to a Gmail account. It is not all my code, (modified from a blog post) but it works. I need to keep a copy of all my email received in all my accounts so I can consolidate them into one main one. In the Outlook 2010 Exchange account, all the forwarded mail gets saved in the SENT folder as a copy.
Is it possible to delete the autoforwarded copy in the SENT folder, without deleting all SENT emails? I need to keep the emails I actually respond to.
I would not have a problem using conversation mode in the INBOX, to store the replied to emails. but as it now stands, everything is duplicated due to the bcc copy in the SENT folder when I toggle Conversation mode for the INBOX.
Thanks in advance for any assistance.
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 = "bcc.hwb#gmail.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_NewMailEx(ByVal EntryIDCollection As String)
Dim varEntryIDs
Dim objItem
Dim myItem As MailItem
Dim i As Integer
varEntryIDs = Split(EntryIDCollection, ",")
For i = 0 To UBound(varEntryIDs)
Set objItem = Application.Session.GetItemFromID(varEntryIDs(i))
'MsgBox (varEntryIDs(i))
Set myItem = objItem.Forward
myItem.Recipients.Add "bcc.hwb#gmail.com"
myItem.Send
'myItem.Delete
Set myItem = Nothing
Next
End Sub
See MailItem.DeleteAfterSubmit Property (Outlook)
myItem.DeleteAfterSubmit = True