troubleshooting Question

Outlook Macro to delete emails not meeting keywords

Avatar of Jose Rivera-Hernandez
Jose Rivera-HernandezFlag for United States of America asked on
Microsoft ApplicationsVB ScriptOutlookRESTVisual Basic Classic
55 Comments1 Solution509 ViewsLast Modified:
I have an Outlook (2010) Macro that sends emails with certain keywords in them to the "Deleted Items" bin.  What I would like now is an Outlook macro that will move emails having certain key words (account, ssn, credit) to another folder and the rest to the "Deleted Items" bin. All these emails come from a defined account.

see the code that I am using right now.

Function UnwantedMessage(olkMsg As Outlook.MailItem) As Boolean
    'On the next line edit the keywords/phrases you want to filter out.  Use the | character to separate each word/phrase
    Const KEYWORDS = "Address change completed|Thank you for your interest in employment |Ad|Internet Explorer 8 Settings"
    Dim olkAtt As Outlook.Attachment, objRegEx As Object, colMatches As Object
    Set objRegEx = CreateObject("VBscript.RegExp")
    With objRegEx
        .IgnoreCase = False
        .Pattern = KEYWORDS
        .Global = True
    End With
    For Each olkAtt In olkMsg.Attachments
        If IsHiddenAttachment(olkAtt) Then
            Set colMatches = objRegEx.Execute(olkAtt.FileName)
            If colMatches.Count > 0 Then
                UnwantedMessage = True
                Exit For
            End If
        End If
    Next
    Set olkAtt = Nothing
    Set objRegEx = Nothing
    Set colMatches = Nothing
End Function
ASKER CERTIFIED SOLUTION
gowflow
Partner

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 55 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 55 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros