Consulting

Results 1 to 7 of 7

Thread: Macro to clear out Folder

  1. #1
    VBAX Newbie
    Joined
    May 2017
    Posts
    5
    Location

    Macro to clear out Folder

    Hi! I'm trying to build a Macro to quickly go through a bunch of disabled rules to clean out a folder in outlook. I am starting with the following code, but there are three issues with it currently.

    1) I want it to only run in a specific folder. I know this is a sub feature of execute rule, but every time I add it in the line turns read and yells "You're a bad coder" at me.
    2) I would like the loop command to only apply to disabled rules
    3) I would like the loop command to ignore one specific rule labelled ruleName. This is coded in there already, but it doesn't appear to work. Several test have verified failure.

    Also, this code is mostly borrowed form another site and this is my first time coding in Outlook, why does the code have Set all the abbreviations to nothing at the end of the code? I haven't seen that in all my code building for Excel.

    Sub RunRules()
    Dim st As Outlook.Store
    Dim myRules As Outlook.Rules
    Dim rl As Outlook.Rule
    Dim count As Integer
    Dim ruleList As String
    Dim ruleName As String
    
    
    ruleName = "Rule"
    
    
    On Error Resume Next 
    Set st = Application.Session.DefaultStore
    Set myRules = st.GetRules 
    
    For Each rl In myRules
        If rl.RuleType = olRuleReceive And rl.Name <> ruleName Then
            rl.Execute ShowProgress:=True, RuleExecuteOption:=1 
            count = count + 1
            ruleList = ruleList & vbCrLf & rl.Name
        End If
    Next
    
    
    Set rl = Nothing
    Set st = Nothing
    Set myRules = Nothing
    End Sub
    Thanks!

  2. #2
    VBAX Newbie
    Joined
    May 2017
    Posts
    5
    Location
    Found the solution the #3. Just have to add a nested if clause.

    For #2, it seems to have to do the with the AccountRuleCondition Enabled. However the following code did not work:

    If AccountRuleCondition.Enabled = False Then
    ' do stuff
    End IF

  3. #3
    VBAX Newbie
    Joined
    May 2017
    Posts
    5
    Location
    Solved #2:

    If Enabled = False Then
    ' do stuff
    end if
    Anyone able to help me select the custom folder I want to run this in?

  4. #4
    VBAX Newbie
    Joined
    May 2017
    Posts
    5
    Location
    Here's the current version. It turns out my fix for the disabled Macros was wrong and I still need help fixing that. Anyone have any insight into how to make it only run disabled rules?

    Sub RunAllInactiveRules()
    '
    '
    '
    ' Created by James Fields
    ' Version 1.0 5-19-17
    
    
    
    
    Dim st As Outlook.Store
    Dim myRules As Outlook.Rules
    Dim rl As Outlook.Rule
    Dim count As Integer
    Dim ruleList As String
    Dim ruleName As String
    
    
    ' This can allow you to ignore a specific rule by the name of your choice.
    ruleName = ""
    
    
    ' Finds the active folder
    Set fld = Application.ActiveExplorer.CurrentFolder
    
    
    On Error Resume Next ' Get default store (where rules live)
    Set st = Application.Session.DefaultStore
    Set myRules = st.GetRules ' Get rules
    
    
    ' Run all inactive rules
    For Each rl In myRules
        If rl.RuleType = olRuleReceive Then
            If rl.Name <> ruleName Then ' Ignores the rule you named previously
                 If Enabled = False Then ' Only runs on disabled rules
                    rl.Execute ShowProgress:=True, Folder:=fld, RuleExecuteOption:=1  ' ExecuteOption 1 only functions on read mail, 2 on unread, and 0 on all.  Default is 0
                    count = count + 1
                    ruleList = ruleList & vbCrLf & rl.Name
                End If
            End If
        End If
    Next
    
    
    Set rl = Nothing
    Set st = Nothing
    Set myRules = Nothing
    Set fld = Nothing
    End Sub

  5. #5
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    This appears to be incorrect

    If Enabled = False Then
    Maybe

    If rl.Enabled = False Then

    On Error Resume Next always has to have a closely following On Error GoTo 0 to turn off the error bypass.

    It is used for expected errors. Remove the line and if there are unexpected errors fix them.
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  6. #6
    VBAX Newbie
    Joined
    May 2017
    Posts
    5
    Location
    You're right about that not working, I caught that a bit later in testing it. And that's the same fix I used to!

    I'm not sure I understand the Error Resume comments, can you elaborate?

  7. #7
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    There are specific times you can safely ignore errors.

    This is the "lazy" way to create a folder without bothering to verify it already exists.

    On Error Resume Next
    MkDir "folder that may already exist"
    On Error GoTo 0
    If the folder exists there is no harm in bypassing the error.

    If you continue without On Error GoTo 0

    On Error Resume Next
    MkDir "folder that may already exist"
    Statement that has to process correctly
    If there is an error in "Statement that has to process correctly" you will not know it failed.
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •