Link to home
Start Free TrialLog in
Avatar of mdadnan
mdadnan

asked on

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!
Avatar of David Lee
David Lee
Flag of United States of America image

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.
Avatar of mdadnan
mdadnan

ASKER

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!
What action do you want the scriptto take if the condition is true?
why dont you just make 2 seperate rules?
one for invoice, and one for company?
Avatar of mdadnan

ASKER

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

Avatar of mdadnan

ASKER

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.
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.  
alright, sorry i couldnt be of help.
Avatar of mdadnan

ASKER

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
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.
Avatar of mdadnan

ASKER

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!!
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
Avatar of mdadnan

ASKER

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.


Are you saying that you want a means of applying the same action to all outgoing emails too?
Avatar of mdadnan

ASKER

yes, if that can also be done throgh the script.
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America 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
Avatar of mdadnan

ASKER

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 :))
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

Avatar of mdadnan

ASKER

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