Use VBA / macro to modify a rule in Outlook ?

Posted on 2013-09-09
Medium Priority
Last Modified: 2013-10-21
Hi.  I am a developer, but not familiar with the Outlook 2013 Object Model.
I have a client that has a small task -- In Outlook, he would like to be able to select an email message in his Inbox, and then click a button to append the sender's address to an existing "move to" rule.

He has a "customers.pst" file;  In that PST file, there is a "New Mail" folder.

In Outlook, he already has a rule defined that moves new emails from a list of senders (by email address)  from his default Inbox to the "Customers\New Mail" folder  (when new email is received.)   It takes many steps to manually add a new customer address to that rule.

I assume this would be a simple VBA macro, wherein I would get the "sender" from a selected message, then add it to the existing rule using a call to "Outlook.Rule.Conditions.From.Recipients.Add" or something like that.

I did find this article:  Create a Rule to Move Specific E-mails to a Folder  but have not been successful in modifying the sample code to meet my needs -- I've made a few attempts, but have not been able to point to the proper PST\Folder or successfully modify the "conditions".  

It seems like it would not take much code, so does anyone have any examples?

-- Thanks
Question by:Rob Rudloff
  • 2
LVL 54

Assisted Solution

Rgonzo1971 earned 1800 total points
ID: 39478761

you could modify this example to change your rule


LVL 21
ID: 39480462
It seems like it would not take much code, so does anyone have any examples?
What about no code?

It I were setting this up I would keep it simple. I would create a rule that uses "Sender in specified address book"

Create a separate "customer" address book for all customers that need to be moved to the "Customers\New Mail" folder.

To update the rule all you have to do is add someone to the "customer" address book.

Ouitlook Rule

Author Comment

by:Rob Rudloff
ID: 39490490
rgonzo1971 --
Yes, that is a good example, and similar to the one in my link.  My problem is that they "create" a new rule:
'Create the rule by adding a Receive Rule to Rules collection
 Set oRule = colRules.Create("Dan's rule", olRuleReceive)
I need to figure out how to iterate through existing rules to find the existing rule "Move To Customers"

-- thanks
LVL 54

Accepted Solution

Rgonzo1971 earned 1800 total points
ID: 39490541

in ColRules you have all the existent rules

you can then access all the items in the locals window so you can choose yours like this not sure of the syntax not at home
    Set colRules = Application.Session.DefaultStore.GetRules() 
    'Create the rule by adding a Receive Rule to Rules collection 
    Set oRule = colRules.Item("Your Rule", olRuleReceive) 
'   or  Set oRule = colRules.Item(2)

Open in new window



or if not working

you could try

bFound = False
Set colRules = Application.Session.DefaultStore.GetRules() 

For Each RuleItem In colRules
    If RuleItem.Name = "your Rule" Then
        Set oRule = RuleItem
        bFound = True
        Exit For
If bFound = False Then
    Msgbox " Rule not found"
    Exit Sub
End If

Open in new window


Featured Post

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

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.

Join & Write a Comment

Organisation is organized in a pattern to flow the day to day business, every application and system is interdepended on each other and when very important “Exchange Server downtime” happened.
Configure external lookups on for external mail flow on Exchange 2013 and Exchange 2016.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Watch the video to know the simple way to remove or recover or reset lost or forgotten passwords of Outlook PST file. With Kernel Outlook Password Recovery tool such operation is very easy to perform. It is a freeware with limitation to use with 500…

587 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