PDA

View Full Version : Macro to clear out Folder



jfslicer
05-17-2017, 04:51 PM
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!

jfslicer
05-18-2017, 11:51 AM
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

jfslicer
05-18-2017, 04:23 PM
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?

jfslicer
05-19-2017, 03:20 PM
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

skatonni
05-23-2017, 02:04 PM
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.

jfslicer
05-23-2017, 04:17 PM
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?

skatonni
06-09-2017, 09:54 AM
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.