PDA

View Full Version : find and remove emails based on BCC input



MarijkeP
10-30-2018, 07:44 AM
Hello,

Can you help me generate a script with these criteria:
- search folder = Sent items
- FROM: "*myself*"
- BCC = "*someone*"
if it meets these criteria, move the email to the 'Deleted Items' folder


Outlook Rules does not provide what I am looking for, as I cannot set the BCC criteria, so hopefully a VBA might be achievable....

Kind regards,
Marijke

gmayor
10-30-2018, 09:27 PM
Unless this is a shared mailbox or you have more than one account using the mailbox then all the items in the default sent folder are from you, in which case you can remove the references to strAddress in the following macro i.e. Change
If olItem.SenderEmailAddress = strAddress And _
olItem.BCC = strBCC Thento
If olItem.BCC = strBCC Then

Sub DeleteSent()
Dim olItems As Outlook.Items
Dim olItem As Outlook.MailItem
Dim i As Long
Dim strAddress As String
Dim strBCC As String

strAddress = "me@myaddress.com" 'Your e-mail address
strBCC = "someone@somewhere.com" 'The BCC address

Set olItems = Session.GetDefaultFolder(olFolderSentMail).Items
olItems.Sort "[Received]", True
For i = olItems.Count To 1 Step -1
Set olItem = olItems(i)
If olItem.SenderEmailAddress = strAddress And _
olItem.BCC = strBCC Then
olItem.Delete
End If
Next i
Cleanup:
Set olItems = Nothing
Set olItem = Nothing
lbl_Exit:
Exit Sub
End Sub

MarijkeP
11-01-2018, 01:53 AM
This is indeed a shared mailbox

I have added to code, but an error appears on this step:

Set olItem = olItems(i)


The error message indicated:
Run-time error '13': Type mismatch

gmayor
11-01-2018, 02:42 AM
It seems that you may have items that are not mailitems (such as meeting requests) in the folder which would cause the error. You could change the declaration type of olItem to 'Object' or you can add some error trapping to check only mail items e.g. as shown below.

If the BCC in question is shown as a name rather than an address, put the name (exactly as it appears) as the value of strBCC.


Sub DeleteSent()
Dim olItems As Outlook.Items
Dim olItem As Outlook.MailItem
Dim i As Long
Dim strAddress As String
Dim strBCC As String

strAddress = "me@myaddress.com" 'Your e-mail address
strBCC = "someone@somewhere.com" 'The BCC address

Set olItems = Session.GetDefaultFolder(olFolderSentMail).Items
olItems.Sort "[Received]", True
For i = olItems.Count To 1 Step -1
If TypeName(olItems(i)) = "MailItem" Then
Set olItem = olItems(i)
If Not olItem.BCC = "" Then
If olItem.SenderEmailAddress = strAddress And _
InStr(1, olItem.BCC, strBCC) > 0 Then
olItem.Delete
End If
End If
End If
Next i
Cleanup:
Set olItems = Nothing
Set olItem = Nothing
lbl_Exit:
Exit Sub
End Sub

MarijkeP
11-08-2018, 01:54 PM
No errors, but Outlook crashed on me.
I'm guessing I simply have too many emails that are being reviewed....

I'm sure there must be a method to select only emails with a certain 'age', so I can work through the sent mails in batches rather than in 1 go. (for example the last 30 days)
Is this indeed possible and would you be able to provide me some directions there?


also, will the script move the emails to the deleted items folder, or will it permanently delete the emails?

gmayor
11-09-2018, 07:06 AM
Add the line
DoEventsimmediately before the line
Next iwhich should stop the process appearing to hang.
The messages are moved to the deleted folder.