Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-03-2021, 06:58 AM
Homepro Homepro is offline Macro to Run Rules Windows 10 Macro to Run Rules Office 2019
Novice
Macro to Run Rules
 
Join Date: Sep 2021
Posts: 1
Homepro is on a distinguished road
Default Macro to Run Rules


I am trying to create a macro so I can run all my rules by clicking one button rather than going thru six steps. I found 1 macro online but it does not work. Has anyone here got one that does?
Reply With Quote
  #2  
Old 09-03-2021, 08:41 PM
gmayor's Avatar
gmayor gmayor is offline Macro to Run Rules Windows 10 Macro to Run Rules Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Create a userform with a single list box large enough to list your rules and name it 'frmRunRules'.
Add two command buttons below the list box.
Add the following code to the userform:
Code:
Option Explicit
Private olStore As Outlook.Store
Private olRules As Outlook.Rules
Private rl As Outlook.Rule
Private Count As Integer
Private olRuleList As String
Private i As Long
Private j As Long
Private temp As Variant

Private Sub CommandButton2_Click()
    Unload Me
lbl_Exit:
    Exit Sub
End Sub

Private Sub UserForm_Initialize()
    Set olStore = Session.DefaultStore
    Set olRules = olStore.GetRules
    BackColor = &HC0E1BF
    Caption = "Select the Rules to Run on the Inbox"
    CommandButton1.Caption = "Run the Selected Rules"
    CommandButton2.Caption = "Cancel"
    
    With ListBox1
        .Clear
        On Error Resume Next
        For Each rl In olRules
            .AddItem rl.Name
        Next rl
        For j = 0 To .ListCount - 2
            For i = 0 To .ListCount - 2
                If UCase(.List(i)) > UCase(.List(i + 1)) Then
                    temp = .List(i)
                    .List(i) = .List(i + 1)
                    .List(i + 1) = temp
                End If
            Next i
        Next j
        .MultiSelect = fmMultiSelectExtended
    End With
    RemoveCloseButton Me
lbl_Exit:
    Exit Sub
End Sub

Private Sub CommandButton1_Click()
    Hide
    Set olStore = Session.DefaultStore
    Set olRules = olStore.GetRules
    With ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                Set rl = olRules.Item(.List(i))
                If rl.RuleType = olRuleReceive Then
                    ' if so, run it
                    rl.Execute showprogress:=True
                    Count = Count + 1
                    olRuleList = olRuleList & vbCrLf & rl.Name
                End If
            End If
        Next i
    End With
    olRuleList = "These rules were executed against the Inbox: " & vbCrLf & olRuleList
    MsgBox olRuleList, vbInformation, "Outlook Rules"

    Set rl = Nothing
    Set olStore = Nothing
    Set olRules = Nothing
    Unload Me
lbl_Exit:
    Exit Sub
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then Cancel = True
lbl_Exit:
    Exit Sub
End Sub
Call the userform from your button with the code -

Code:
Sub RunMyRules()
    frmRunRules.Show
lbl_Exit:
    Exit Sub
End Sub
When run, select the rules you wish to run on the Inbox from the list box.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rules or Add-in mosheva Outlook 0 06-11-2017 04:06 AM
Macro to Run Rules Macro to replace text and track changes based on user-defined rules Thefirstfish` Word VBA 13 01-09-2016 07:14 AM
Rules iflog Outlook 0 11-28-2013 07:32 AM
I can't get to my old rules from 2007 jtpryan Outlook 1 05-15-2011 03:29 PM
Question about Tools>Rules and Alerts>E-mail Rules RichGuard Outlook 0 09-02-2010 05:19 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:09 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft