VBA function to programmatically create Outlook rule to run a script RRS feed

  • Question

  • Hi 

    I am creating a macro in Excel that on a button click will set rules in Outlook.

    My code so far is:

    Private Sub CommandButton1_Click()
        Call CreateRule
    End Sub
    Sub CreateRule()
        Dim oloUtlook As Outlook.Application
        Dim ns As Outlook.Namespace
        Dim colRules As Outlook.Rules
        Dim oRule As Outlook.Rule
        Dim colRuleActions As Outlook.RuleAction
        Dim oMoveRuleAction As Outlook.RuleCondition
        Dim oM As Outlook.MoveOrCopyRuleAction
        Dim oFromCondition As Outlook.ToOrFromRuleCondition
        Dim oExceptSubject As Outlook.TextRuleCondition
        Dim oInbox As Outlook.Folder
        Dim oMoveTarget As Outlook.Folder
        Dim myItem As Outlook.MailItem
        Dim oRunSc
                    Set oloUtlook = CreateObject("Outlook.Application")
                    Set ns = oloUtlook.GetNamespace("MAPI")
                    Set oDeletedItems = ns.GetDefaultFolder(olFolderDeletedItems)
                    Set colRules = ns.DefaultStore.GetRules()
                    Set oRule = colRules.Create("Calendar Rule", olRuleReceive)
                    Set oMoveRuleAction = oRule.Conditions.MeetingInviteOrUpdate
                            With oMoveRuleAction
                                .Enabled = True
                            End With
                    Set colRuleActions = oRule.Actions.Delete
                        With colRuleActions
                            .Enabled = True
                        End With
    End Sub

    Running this creates the rules in Outlook, but I also want to and an action of running a script but I cannot find documentation for this anywhere. Could someone point me in the right direction ? I assume I will have to add to my colRuleActions...?

    Thursday, November 5, 2015 9:21 PM


  • The olRuleActionRunScript value is not supported when new rules are created programmatically. Instead, you can handle the Outlook object model events in Excel's VBA code. Or just develop an Outlook add-in which you can reach using the VBA code and enabled any processing of incoming emails. 
    Friday, November 6, 2015 5:00 PM

All replies