Solved

Custom macro rule in Outlook 2007

Posted on 2011-03-13
22
616 Views
Last Modified: 2013-11-05
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.
0
Comment
Question by:Wilder1626
  • 11
  • 11
22 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35121474
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
 
LVL 11

Author Comment

by:Wilder1626
ID: 35121518
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35121541
Are you ok with creating a rule on all incoming mails that calls a script?

Chris
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 35121584
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35121586
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
 
LVL 11

Author Comment

by:Wilder1626
ID: 35121592
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35121593
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35121597
Note the line:

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

Chris
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 35121601
ok, i will try it now.

I will let you know the result.

Thanks
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 35121628
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35121638
Indeed and depending on where the folder unwanted is located ... can you advise it's path

Chris
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Author Comment

by:Wilder1626
ID: 35121656
yes, it is in : \\Dossiers personnels

Folder name: Courrier indésirable
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 35121680
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
 
LVL 11

Author Comment

by:Wilder1626
ID: 35121717
wow, one last question.

Can it go under a  sub folder:\\Dossiers personnels\Courrier indésirable
Folder name: Test email
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35121733
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
 
LVL 11

Author Comment

by:Wilder1626
ID: 35121749
i have created the sun folder "Test email" but it keeps going in the "Courrier indésirable" folder
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35121761
Is test email a sub folder of Courrier indésirable or t the same level

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35121766
If it is a sub folder then can you supply your code as it is at the moment

Chris
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 35121768
it is really a sub folder

Courrier indésirable
 - Test email

Deleted items
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 35122365
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
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 35122778
Thanks for your help.

I will take this one.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35123075
I guess you have a rule that is doing that move over and above the one with the call to the script?

Chris
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Find out how to use dynamic social media in email signatures with this top 10 DOs & DON’Ts.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now