Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

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.
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Wilder1626

ASKER

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.
Are you ok with creating a rule on all incoming mails that calls a script?

Chris
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.
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

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.
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
Note the line:

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

Chris
ok, i will try it now.

I will let you know the result.

Thanks
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
Indeed and depending on where the folder unwanted is located ... can you advise it's path

Chris
yes, it is in : \\Dossiers personnels

Folder name: Courrier indésirable
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
wow, one last question.

Can it go under a  sub folder:\\Dossiers personnels\Courrier indésirable
Folder name: Test email
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
i have created the sun folder "Test email" but it keeps going in the "Courrier indésirable" folder
Is test email a sub folder of Courrier indésirable or t the same level

Chris
If it is a sub folder then can you supply your code as it is at the moment

Chris
it is really a sub folder

Courrier indésirable
 - Test email

Deleted items
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.
Thanks for your help.

I will take this one.
I guess you have a rule that is doing that move over and above the one with the call to the script?

Chris