Small vba to kill political email

Status
Not open for further replies.

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
 
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
 
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
 
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
 
Status
Not open for further replies.
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
H using VBA to edit subject line Outlook VBA and Custom Forms 0
G Get current open draft message body from VBA Outlook VBA and Custom Forms 1
Geldner Problem submitting SPAM using Outlook VBA Form Outlook VBA and Custom Forms 2
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
M Outlook 2016 outlook vba to look into shared mailbox Outlook VBA and Custom Forms 0
V VBA Categories unrelated to visible calendar and Visual appointment Categories Outlook VBA and Custom Forms 2
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 3
R Outlook 365 VBA AUTO SEND WITH DELAY FOR EACH EMAIL Outlook VBA and Custom Forms 0
R Outlook 2019 VBA to List Meetings in Rooms Outlook VBA and Custom Forms 0
geoffnoakes Counting and/or listing fired reminders via VBA Using Outlook 1
O VBA - Regex - remove double line spacing Outlook VBA and Custom Forms 1
D.Moore Strange VBA error Outlook VBA and Custom Forms 4
B Modify VBA to create a RULE to block multiple messages Outlook VBA and Custom Forms 0
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
D VBA - unable to set rule condition 'on this computer only' Outlook VBA and Custom Forms 5
L Fetch, edit and forward an email with VBA outlook Outlook VBA and Custom Forms 2
BartH VBA no longer working in Outlook Outlook VBA and Custom Forms 1
W Can vba(for outlook) do these 2 things or not? Outlook VBA and Custom Forms 2
MattC Changing the font of an email with VBA Outlook VBA and Custom Forms 1
P MailItem.To Property with VBA not work Outlook VBA and Custom Forms 2
P Tweak vba so it can target another mailbox Outlook VBA and Custom Forms 1
A Outlook 2010 VBA fails to launch Outlook VBA and Custom Forms 2
richardwing Outlook 365 VBA to access "Other Actions" menu for incoming emails in outlook Outlook VBA and Custom Forms 0
W Create a Quick Step or VBA to SAVE AS PDF in G:|Data|Client File Outlook VBA and Custom Forms 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 1
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

Similar threads

Back
Top