Custom macro rule in Outlook 2007

Hello all,

I would like to know if it is possible to create a custom macro rule that if specific words appear in the sender name, the object, the body and attachment, to automatically deleted the email.

Please let me know.

Thanks for your help.
LVL 11
Wilder1626Asked:
Who is Participating?
 
Chris BottomleyConnect With a Mentor Software Quality Lead EngineerCommented:
Is that the default PST?  If so the following should work ... but i'm not sure what happens with inflexions in folder naming!

If you want the saved mail highlighted rather than marked as read then delete the line:

        mai.UnRead = False

Chris
Sub Q_26883221(mai As MailItem)
Dim strFind As Variant
Dim blnNoGood As Boolean
Dim att As Attachment

    For Each strFind In Array("Casino", "Gambler")
        If InStr(1, mai.SenderName, strFind, vbTextCompare) > 0 Then
            blnNoGood = True
            Exit For
        ElseIf InStr(1, mai.Subject, strFind, vbTextCompare) > 0 Then
            blnNoGood = True
            Exit For
        ElseIf InStr(1, mai.body, strFind, vbTextCompare) > 0 Then
            blnNoGood = True
            Exit For
        Else
            For Each att In mai.Attachments
                If InStr(1, att.name, strFind, vbTextCompare) > 0 Then
                    blnNoGood = True
                    Exit For
                End If
            Next
            If blnNoGood Then Exit For
        End If
    Next
    If blnNoGood Then
        mai.UnRead = False
        mai.Move Application.Session.folders("Courrier indésirable")
    End If

End Sub

Open in new window

0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Yes is the short answer.  Can you clarify what you require:

If specific word(s) appear in the Sender name or the body or any attachments, (name or contents?) then delete the mail?

Chris
0
 
Wilder1626Author Commented:
Great, here is what i need.



I need to be able to put a list of all the specific words that macro will filter on all these email fields:
.From
.Subject
.Body
.Attachments.Name


Ex: Casino, Gambler ..........


So if one of these words are in one of these fields, it will automatically deleted the email.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Chris BottomleySoftware Quality Lead EngineerCommented:
Are you ok with creating a rule on all incoming mails that calls a script?

Chris
0
 
Wilder1626Author Commented:
I no how to create regular rules but not that are related with a macro.

I was already using the outlook rule that was deleting words in the .Subject.

But if i can manage all the fields at once, it would be so good.
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
If so then add the following script to a normal module and call it from the rule ... i.e. Q_26883221

Chris
Sub Q_26883221(mai As MailItem)
Dim strFind As Variant
Dim blnNoGood As Boolean
Dim att As Attachment

    For Each strFind In Array("Casino", "Gambler")
        If InStr(1, mai.SenderName, strFind, vbTextCompare) > 0 Then
            blnNoGood = True
            Exit For
        ElseIf InStr(1, mai.Subject, strFind, vbTextCompare) > 0 Then
            blnNoGood = True
            Exit For
        ElseIf InStr(1, mai.body, strFind, vbTextCompare) > 0 Then
            blnNoGood = True
            Exit For
        Else
            For Each att In mai.Attachments
                If InStr(1, att.name, strFind, vbTextCompare) > 0 Then
                    blnNoGood = True
                    Exit For
                End If
            Next
            If blnNoGood Then Exit For
        End If
    Next
    If blnNoGood Then mai.Delete
    
End Sub

Open in new window

0
 
Wilder1626Author Commented:
also, in that regular rule i have, i have about 70 words.

So it is very long to enter one by one in the rule.

It would be easier with a custom macro to also put the word list.
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
When creating your normal rule select all messages and then in actions instead of looking at the subject scroll down the actions box a bit further ... you will see run a script select that option instead and double click the hyperlink and the sub you added above should be visible.

Chris
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Note the line:

    For Each strFind In Array("Casino", "Gambler")
 
Keep adding your words here i.e. "Casino", "Gambler", "another", "fifty", "Words")

Chris
0
 
Wilder1626Author Commented:
ok, i will try it now.

I will let you know the result.

Thanks
0
 
Wilder1626Author Commented:
wow this is working

Small question, what do i need to do if instead of deleting it, i want the macro to send it into the unwanted folder?

i guess i would change this: mai.Delete
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Indeed and depending on where the folder unwanted is located ... can you advise it's path

Chris
0
 
Wilder1626Author Commented:
yes, it is in : \\Dossiers personnels

Folder name: Courrier indésirable
0
 
Wilder1626Author Commented:
wow, one last question.

Can it go under a  sub folder:\\Dossiers personnels\Courrier indésirable
Folder name: Test email
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
If I understand:

mai.Move Application.Session.folders("Courrier indésirable")
becomes
mai.Move Application.Session.folders("Courrier indésirable").folders("Test email")

But that folder needs to exist already.

Chris
0
 
Wilder1626Author Commented:
i have created the sun folder "Test email" but it keeps going in the "Courrier indésirable" folder
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Is test email a sub folder of Courrier indésirable or t the same level

Chris
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
If it is a sub folder then can you supply your code as it is at the moment

Chris
0
 
Wilder1626Author Commented:
it is really a sub folder

Courrier indésirable
 - Test email

Deleted items
0
 
Wilder1626Author Commented:
i see that even if a remove this part of the code:
 mai.Move Application.Session.folders("Courrier indésirable")

Open in new window


it still always go in  .folders("Courrier indésirable")

I don't understand why.
0
 
Wilder1626Author Commented:
Thanks for your help.

I will take this one.
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
I guess you have a rule that is doing that move over and above the one with the call to the script?

Chris
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.