【发布时间】:2020-01-04 01:58:41
【问题描述】:
当我从 Outlook 2016 迁移到 365 时,我的宏停止工作。我正在寻找对以下代码的更正,以帮助我修复宏以分别启用/禁用和运行所有规则。
Option Explicit
Sub ThePurge()
Dim st As Outlook.Store
Dim myRules As Outlook.Rules
Dim rl As Outlook.Rule
Dim runrule As String
Dim rulename As String
Dim rulelist As String
Dim oDeletedItems As Outlook.Folder
Dim oFolders As Outlook.Folders
Dim oItems As Outlook.Items
Dim i As Long
rulename = "ThePurge"
Set st = Application.Session.DefaultStore
Set myRules = st.GetRules
For Each rl In myRules
If rl.RuleType = olRuleReceive Then
If rl.Name = rulename Then
rl.Execute ShowProgress:=True, IncludeSubfolders:=True
runrule = rl.Name
End If
End If
Next
rulelist = "This rule was executed against the Inbox and Subfolders:" &
vbCrLf & runrule
MsgBox rulelist, vbInformation, "Macro: The PURGE"
'Obtain a reference to deleted items folder
Set oDeletedItems =
Application.Session.GetDefaultFolder(olFolderDeletedItems)
Set oItems = oDeletedItems.Items
For i = oItems.count To 1 Step -1
oItems.Item(i).Delete
Next
Set oFolders = oDeletedItems.Folders
For i = oFolders.count To 1 Step -1
oFolders.Item(i).Delete
Next
Set rl = Nothing
Set st = Nothing
Set myRules = Nothing
End Sub
Sub DisableAllRules()
Dim colRules As Outlook.Rules
Dim oRule As Outlook.Rule
Dim count As Integer
Dim rulelist As String
'On Error Resume Next
'Get Rules from Session.DefaultStore object
Set colRules = Application.Session.DefaultStore.GetRules
' iterate all the rules
For Each oRule In colRules
oRule.Enabled = False
count = count + 1
rulelist = rulelist & vbCrLf & count & ". " & oRule.Name
Next
colRules.Save
' tell the user what you did
rulelist = "These rules were disabled: " & vbCrLf & rulelist
MsgBox rulelist, vbInformation, "Macro: DisableAllRules"
Set colRules = Nothing
Set oRule = Nothing
End Sub
Sub EnableAllRules()
Dim colRules As Outlook.Rules
Dim oRules As Outlook.Rules
Dim oRule As Outlook.Rule
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Dim count As Integer
Dim rulelist As String
Dim blnExecute As Boolean
'On Error Resume Next
'Get Rules from Session.DefaultStore object
Set colRules = Application.Session.DefaultStore.GetRules
' iterate all the rules
For Each oRule In colRules
oRule.Enabled = True
count = count + 1
rulelist = rulelist & vbCrLf & count & ". " & oRule.Name
Next
colRules.Save
' tell the user what you did
rulelist = "These rules were enabled: " & vbCrLf & rulelist
MsgBox rulelist, vbInformation, "Macro: EnableAllRules"
Set olRules = Application.Session.DefaultStore.GetRules
Set olRule = olRules.Item("ThePurge")
olRule.Enabled = False
If blnExecute Then olRule.Execute ShowProgress:=True
olRules.Save
Set olRules = Nothing
Set olRule = Nothing
Set colRules = Nothing
Set oRule = Nothing
End Sub
Sub RunAllRules()
Dim colRules As Outlook.Rules
Dim oRule As Outlook.Rule
Dim olRule As Outlook.Rule
Dim olRules As Outlook.Rules
Dim count As Integer
Dim rulelist As String
Dim blnExecute As Boolean
'On Error Resume Next
'Get Rules from Session.DefaultStore object
Set colRules = Application.Session.DefaultStore.GetRules
' iterate all the rules
For Each oRule In colRules
oRule.Enabled = True
oRule.Execute ShowProgress:=True
count = count + 1
rulelist = rulelist & vbCrLf & count & ". " & oRule.Name
Next
colRules.Save
' tell the user what you did
rulelist = "These rules were Enforced: " & vbCrLf & rulelist
MsgBox rulelist, vbInformation, "Macro: Run ALL Rules"
Set olRules = Application.Session.DefaultStore.GetRules
Set olRule = olRules.Item("ThePurge")
olRule.Enabled = False
If blnExecute Then olRule.Execute ShowProgress:=True
olRules.Save
Set colRules = Nothing
Set oRule = Nothing
End Sub
Sub EmptyTrash()
Dim oDeletedItems As Outlook.Folder
Dim oFolders As Outlook.Folders
Dim oItems As Outlook.Items
Dim i As Long
'Obtain a reference to deleted items folder
Set oDeletedItems =
Application.Session.GetDefaultFolder(olFolderDeletedItems)
Set oItems = oDeletedItems.Items
For i = oItems.count To 1 Step -1
oItems.Item(i).Delete
Next
Set oFolders = oDeletedItems.Folders
For i = oFolders.count To 1 Step -1
oFolders.Item(i).Delete
Next
End Sub
当我尝试运行此规则时,我在此代码的 sn-p 上收到调试错误(说明运行时错误。一个或多个规则存在冲突):
Set colRules = Application.Session.DefaultStore.GetRules
【问题讨论】:
-
GetRules 是返回一个集合还是为空?
-
获取运行时错误“一个或多个规则发生冲突。您必须先解决冲突才能保存规则。此代码的 sn-p 突出显示:Set colRules = Application .Session.DefaultStore.GetRules