• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 79
  • Last Modified:

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
0
Shujaat Ali
Asked:
Shujaat Ali
  • 3
1 Solution
 
AlanConsultantCommented:
Hi,

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.

Thanks,

Alan.
0
 
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.
0
 
AlanConsultantCommented:
Hi,

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.

Alan.
0
 
Rgonzo1971Commented:
Hi,

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
 Next
 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")
    .Recipients.ResolveAll

 End With
 Set oMoveRuleAction = oRule.Actions.MoveToFolder

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

 colRules.Save True

End Sub

Open in new window

Regards
0
 
AlanConsultantCommented:
Looks to be a solution.
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now