Excel VBA to create Outlook Rule

I need to create an Excel VBA. This VBA will create an outlook rule, which will move the email where address "test@example.com" is in to or cc field

Please help me
Shujaat AliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Why do you specifically want to do this from Excel?

What is it, that is stopping you from doing it in Outlook itself?

Just seems to be making life deliberately difficult, but I am guessing you must have some good reason - I just can't think what that would be.


Shujaat AliAuthor Commented:
It is just because it is a part of an excel project, where the data is coming from excel email, so considering the load of emails, the excel file automatically creates rules to manage the emails and use them appropriately

Please share if you have any solution for this.

Sorry - I can't think of a way to create an Outlook rule from Excel.

Hopefully someone else will post who knows how to do this.


Maybe something like this
Sub CreateRule()
Dim oFromCondition As Object
 Set OutApp = CreateObject("Outlook.Application")

 Set oInbox = OutApp.Session.GetDefaultFolder(6) 'olFolderInbox
 For Each f In oInbox.Folders
    Debug.Print f.Name
 Set oMoveTarget = oInbox.Parent.Folders("Perso")

 Set colRules = OutApp.Session.DefaultStore.GetRules()

 Set oRule = colRules.Create("My rule", 0) 'olRuleReceive

 Set oToCondition = oRule.Conditions.SentTo

 With oToCondition
    .Enabled = True
    .Recipients.Add ("test@example.com")

 End With
 Set oMoveRuleAction = oRule.Actions.MoveToFolder

 With oMoveRuleAction
    .Enabled = True
    .Folder = oMoveTarget
 End With

 colRules.Save True

End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Looks to be a solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.