Small vba to kill political email

TedSch

New Member
Outlook version
Outlook 2016 64 bit
Email Account
IMAP
I am on every political email list asking for money. Even though I unsubscribed, they were still coming in different candidates. There are a few major companies (political SaaS) that swap your name around to the difference candidates even when you are not living in the same state. If you look at the email's HTML source, you'll see ActBlue, Npvan, Sendgrid as the main ones if you are a democrat. There doesn't seem to be a way to make a rule that acts on hidden HTML source test. Below you'Il see the code I assigned to a button on the quick access tool bar. It works. It is easy to expand the array for any newcomers. Let me know if there is a better way. Would there be a way to make this a custom rule that operates when new emails arrive? Thank you Slipstick for being the go-to for Outlook.

Public Function ArrayTest(strToTest As String) As Boolean
Dim strTxtArray() As Variant
Dim k As Integer

strToTest = UCase(strToTest)

strTxtArray() = Array("ACTBLUE", "SENDGRID", "ACTIONNETWORK", "NPGVAN", "ACTIONKIT", "USO.ORG", "AMERICARES.ORG", "SIERRACLUB.ORG")

For k = LBound(strTxtArray) To UBound(strTxtArray)
'Debug.Print strTxtArray(k)
'Debug.Print strToTest
If InStr(strToTest, strTxtArray(k)) > 0 Then
ArrayTest = True
Exit For
Else
ArrayTest = False
End If
Next k
End Function

Public Sub DelPoliticalSpam()
Dim oInboxFolder As MAPIFolder
Dim obj As Object
Dim olApp As Outlook.Application
Dim objExpl As Outlook.Explorer
Dim strBodyHTML As String
Dim strTypeName As String
'On Error Resume Next
Set olApp = Application
Set objExpl = olApp.ActiveExplorer
Set oInboxFolder = ReturnFolder(strInbox1, olFolderInbox)

For Each obj In oInboxFolder.Items
strTypeName = TypeName(obj)
' Debug.Print strTypeName
If strTypeName = "MailItem" Then
strBodyHTML = obj.HTMLBody
If ArrayTest(strBodyHTML) Then
'MsgBox (strBodyHTML)
obj.Delete
End If
End If
Next obj
'return to inbox
objExpl.SelectFolder ReturnFolder(strInbox1, olFolderInbox)
Set obj = Nothing
Set oInboxFolder = Nothing

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
To run it as a rule - see

This should work -
Public Sub DeleteSpamMessage(Item As Outlook.MailItem)
Dim strBodyHTML As String
strBodyHTML = item.HTMLBody
If ArrayTest(strBodyHTML) Then
'MsgBox (strBodyHTML)
item.Delete
End If
End Sub
 

TedSch

New Member
Outlook version
Outlook 2016 64 bit
Email Account
IMAP
Thanks - I've been a fan of yours for a long time!
If I deleted the object/item in the for loop, I found that the previous version missed deleting about 1/2 because it shortened the collection. So, I added the items to be deleted to a new collection and then used a For to delete items in the new collection. That got them all.

Public Sub DelPoliticalSpam()
Dim oInboxFolder As MAPIFolder
Dim obj As Object
Dim olApp As Outlook.Application
Dim objExpl As Outlook.Explorer
Dim strBodyHTML As String
Dim strTypeName As String
Dim strSubject As String
Dim collItems As New Collection
On Error Resume Next
Set olApp = Application
Set objExpl = olApp.ActiveExplorer
Set oInboxFolder = ReturnFolder(strInbox1, olFolderInbox)

For Each obj In oInboxFolder.Items
strTypeName = TypeName(obj)
Debug.Print CStr(k) & ": " & strTypeName
If strTypeName = "MailItem" Then
strBodyHTML = obj.HTMLBody
strSubject = obj.Subject
If ArrayTest(strBodyHTML, strSubject) Then
collItems.Add obj
'obj.Delete
End If
End If
Next obj

For Each obj In collItems
strTypeName = TypeName(obj)
If strTypeName = "MailItem" Then
strBodyHTML = obj.HTMLBody
strSubject = obj.Subject
If ArrayTest(strBodyHTML, strSubject) Then
obj.Delete
End If
End If
Next obj

'return to inbox
objExpl.SelectFolder ReturnFolder(strInbox1, olFolderInbox)
Set obj = Nothing
Set oInboxFolder = Nothing
Set collItems = Nothing

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I found that the previous version missed deleting about 1/2 because it shortened the collection.
Yeah, I didn't pick up on that when I looked at the macro. When deleting (or moving), you need to count backwards or it will skip every other message - if you have 5 and delete the first, message #2 is the former message #3.

For each works fine if you are flagging, categorizing - just not when moving and deleting.

Get the count then step back -

Dim intCount As Integer

For intCount = oInboxFolder.Items.Count To 1 Step -1
Set obj = oInboxFolder.Items.Item(intCount)
' do whatever
next
 
Similar threads
Thread starter Title Forum Replies Date
A OutLook For Mac 16.46 Comes Up In Small Window When Opening Using Outlook 4
N Outlook 2010 prints in small fonts Using Outlook 1
J Office 365 small business/Outlook 2013 issues Using Outlook.com accounts in Outlook 1
N How to install BCM for Outlook 2013 in Office 365 Small Business Premium BCM (Business Contact Manager) 1
P Outlook 2010 Font on Reply Email is Very Small Ruler Will Not Extend Using Outlook 2
J Microsoft Outlook 2007 Font Size Too Small Using Outlook 1
M Add small calendar to navigation Using Outlook 1
M Font Size Too Small When Replying and Sendig Emails on Outlook 2007 Using Outlook 1
S Creating a Reservation Calendar for small business Using Outlook 1
C My Outlook PST size is about 19GB, how can I seperate it into small size of psts? Using Outlook 3
G Small Businesses Owners being priced out of BCM 2010 BCM (Business Contact Manager) 2
J Outlook opens in a small window Using Outlook 4
G Is Office Small Business 2003 compatible with Windows 7? BCM (Business Contact Manager) 1
O BCM didn't install w small business outlook 2007 BCM (Business Contact Manager) 1
V I cannot open my outlook small business 2007 BCM (Business Contact Manager) 1
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
C Outlook (desktop app for Microsoft365) restarts every time I save my VBA? Using Outlook 0
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
E Outlook 365 Outlook/VBA Outlook VBA and Custom Forms 11
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
Z VBA Forward vs manual forward Outlook VBA and Custom Forms 2
J VBA Cannot programmatically input or change Value for User Defined field Using Outlook 1
J VBA for outlook to compare and sync between calendar Outlook VBA and Custom Forms 1
A Any way to force sort by/group by on search results with VBA? Outlook VBA and Custom Forms 1
E Default shape via VBA Outlook VBA and Custom Forms 4
A Change settings Send/receive VBA Outlook VBA and Custom Forms 0
Z Import Tasks from Access Using VBA including User Defined Fields Outlook VBA and Custom Forms 0
E Outlook VBA change GetDefaultFolder dynamically Outlook VBA and Custom Forms 6
justicefriends How to set a flag to follow up using VBA - for addressee in TO field Outlook VBA and Custom Forms 11
M add new attendee to existing meetings with VBA Outlook VBA and Custom Forms 5
D VBA code to select a signature from the signatures list Outlook VBA and Custom Forms 3
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 2
David McKay VBA to manually forward using odd options Outlook VBA and Custom Forms 1
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
S vba outlook search string with special characters Outlook VBA and Custom Forms 1
S VBA search string with special characters Outlook VBA and Custom Forms 1
U Outlook 2019 VBA run-time error 424 Outlook VBA and Custom Forms 2
DDB VBA to Auto Insert Date and Time in the signature Outlook VBA and Custom Forms 2
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 1
K Outlook Office 365 VBA download attachment Outlook VBA and Custom Forms 2
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
N Save selected messages VBA does not save replies and/or messages that contain : in subject Outlook VBA and Custom Forms 1
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
V vBA for searching a cell's contents in Outlook and retrieving the subject line Outlook VBA and Custom Forms 1
B vBA for exporting excel file from outlook 2016 Outlook VBA and Custom Forms 3

Similar threads

Top