How to filter an email in Outlook 2007 (Rule Management) having two required words in Subject irrespective of their order.

Hi,

I am trying to create a rule for lets say two words in the Subject field "Invoice" and "Company" but here is what is happening.

1. If I list them as "Invoice Company" then the rule search words in that order.
2. If I list them as "Invoice" and "Company" two separate words then the Outlook marks them with OR, which is wrong.

What I need is like an AND so if these two words appear ANYWHERE in the subject gets filtered.

How can this be done?

Thanks!
mdadnanAsked:
Who is Participating?
 
David LeeCommented:
Yes, that's doable.  However, given that the way to do that is to monitor Sent Items and the code is moving things to Sent Items, I'm not sure I see the point.  That aside, here's the code.  This code must go in the ThisOutlookSession module.
Dim WithEvents olkSentItems As Outlook.items
 
Private Sub olkSentItems_ItemAdd(ByVal Item As Object)
    If Item.Class = olMail Then
        MoveMessage2Folder Item
    End If
End Sub
 
Private Sub Application_Startup()
    Set olkSentItems = Session.GetDefaultFolder(olFolderSentMail).Items
End Sub
 
Private Sub Application_Quit()
    Set olkSentItems = Nothing
End Sub

Open in new window

0
 
David LeeCommented:
Hi, mdadnan.

It cannot be done with rules alone.  Rules are rudimentary and there is no AND operation.  You can do it with a rule and a script.  I'll be glad to post sample code if you want to see how this is done.
0
 
mdadnanAuthor Commented:
Yes please, I am quite familiar with Macros and scripts in Excel and will be able to make it work on Outlook as well.

Thanks!
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
David LeeCommented:
What action do you want the scriptto take if the condition is true?
0
 
Newkid34Commented:
why dont you just make 2 seperate rules?
one for invoice, and one for company?
0
 
mdadnanAuthor Commented:
Hi BlueDevilFan,

I want the email to be removed to another folder lets say 'Sent Items'.


To
llewkid34,
Thanks for the response, but my requirement is to have both of the required keywords in the Subject in any unspecified order. Your solution will also filter the emails which are not required. Btw, I have already tested it and is what we don't need.
0
 
David LeeCommented:
Here's the code.  You indicate that you're familiar with macros, so I'm not including detailed instructions.  To use this, create a rule to check messages as they arrive.  Set the rule's action to run a script and choose this script.  The script checks the subject of the message for the words "invoice" and "company".  If both are there, then it moves the message to Sent Items.
Sub MoveMessage2Folder(Item As Outlook.MailItem)
    If InStr(1, LCase(Item.Subject), "invoice") And InStr(1, LCase(Item.Subject), "company") Then
        Item.Move Session.GetDefaultFolder(olFolderSentMail)
    End If
End Sub

Open in new window

0
 
mdadnanAuthor Commented:
Thanks for the response.

Can you confirm if it requires two keywords in lowercase; seems like from your code.

In that case, how can I use Exact Case for 'Invoice' & 'Company' because thats how the emails are formatted.

Thanks again.
0
 
David LeeCommented:
No, it doesn't require lowercase.  I'm forcing the subject line into lower case during the test to avoid have to test for upper, lower, and mixed cases.  In other words, forcing the subject to lower case will find Invoice, invoice, or INVOICE.  
0
 
Newkid34Commented:
alright, sorry i couldnt be of help.
0
 
mdadnanAuthor Commented:
BlueDevilFan: Sorry, but still struggling in setting up the rule. I am running this script for Sent Mail items and modified the script for my requiremet.

But I can't see the option to run a script, in the rule wizard window. (Attached screenshot)

Your script is already entered in Macros, so just need to setup the script rule and test it.

Using Outlook 2007 all updated.

Thanks alot!
outlook97rules.jpg
0
 
David LeeCommented:
You're looking at the conditions page.  The condition will have to be any message sent to the mailbox.  The option to run a cript will be on the next screen, where you shoose the action.
0
 
mdadnanAuthor Commented:
Hi,

Sounding a complete noob, I haven't been able to fix it still.

Would it be possible that you can let me know the options that I should choose from the Condition chosing screen and the next screen.

I have tried a few relevant options without any success.

Thanks!!
0
 
David LeeCommented:
1.  Create a new rule.
2.  Check messages when they arrive
3.  Choose any condition that will cause the rule to fire for every message.  Examples are "on this machine only" and "through the specified account" (assuming that you have just one account).
4.  Choose the "run a script" action.
5.  Select this script as the script to run
0
 
mdadnanAuthor Commented:
Thanks, it seems to be working just fine.

Can you also recommend change in code so for all incoming emails an event is triggered, so all emails from folder 'Sent Items' will be moved to another folder 'Sent Invoices' by running a Rule on 'Sent Items' folder?

I have structured it like this because the 'run script' function is not available for Sent-out emails, only for incoming.

I have increased the points.


0
 
David LeeCommented:
Are you saying that you want a means of applying the same action to all outgoing emails too?
0
 
mdadnanAuthor Commented:
yes, if that can also be done throgh the script.
0
 
mdadnanAuthor Commented:
Hi,

One last request. Although your Answer is 100% complete, can I require slight adjustment.

Your code will monitor all outgoing emails and send them to Sentmail folder, without observing any requirements in the Subject.

I have created another folder under 'Sent Items' called 'Invoice Items'. If you combine all your code together, then what I am requesting is to check for all outgoing emails with subject keywords 'Invoice' and 'Company' appearing anywhere and moving them to subfolder 'Invoice Items'.

Thanks! (almost there :))
0
 
David LeeCommented:
No problem.  Replace the subroutine named MoveMessage2Folder with the revised version below.
Sub MoveMessage2Folder(Item As Outlook.MailItem)
    If InStr(1, LCase(Item.Subject), "invoice") And InStr(1, LCase(Item.Subject), "company") Then
        Item.Move Session.GetDefaultFolder(olFolderSentMail).Folders("Invoice Items")
    End If
End Sub

Open in new window

0
 
mdadnanAuthor Commented:
Perfect. It is all setup.

I am also assuming that I dont have to create a rule for it and all actions will be performed on every new Outlook session.

No need to respond just confirming.

Thanks for all the effort!
0
 
David LeeCommented:
Correct.  You don't need a rule for items you send.  You will need  a rule or a modification to the code to handle inbound items though.
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.