Small Outlook macro that enables or disables an email rule when a named appointment reminder fires.
The auto-enable and auto-disable parts are working correctly, but I am running into a problem when I try to auto-dismiss the reminder afterwards. I get a runtime error '-2147024809 (80070057)'
with olRemind(i).Dismiss
highlighted.
I am pretty sure it is throwing an error because the reminder hasn't actually shown up in the list of reminders yet. However, when I check ?olRemind(i)
in the Immediate window it does return the correct caption (TEST). So it seems like the reminder both does and does not exist?
When I halt code execution, the reminder pops up and the email rule is auto-enabled (or disabled), so I know the rest of the code is working fine.
My hypothesis is that I need to refresh either the list of Reminder objects or the application itself (something similar to Excel's Application.ScreenUpdating), which is why I called DoEvents, but no dice. I couldn't find another method or property that does this in Outlook.
Public WithEvents olRemind As Outlook.Reminders
Private Sub Application_Reminder(ByVal Item As Object)
Set olRemind = Outlook.Reminders
Dim i As Integer
If Item.MessageClass <> "IPM.Appointment" Then
Exit Sub
End If
If Item.Subject = "Enable TEST" Then
Call OnOffRunRule("TEST", True, False)
DoEvents
'Wait 5 seconds
Wait (5)
'Dismiss reminder
For i = olRemind.Count To 1 Step -1
If olRemind(i).Caption = "Enable TEST" Then
'***THE FOLLOWING LINE CAUSES A RUNTIME ERROR***
olRemind(i).Dismiss
End If
Next
End If
If Item.Subject = "Disable TEST" Then
Call OnOffRunRule("TEST", False, False)
DoEvents
'Wait 5 seconds
Wait (5)
'Dismiss reminder
'***THE FOLLOWING LINE CAUSES A RUNTIME ERROR***
Application.Reminders("Disable TEST").Dismiss
End If
End Sub 'Application_Reminder
'Enable or disable a rule
Sub OnOffRunRule(RuleName As String, Enable As Boolean, Optional blnExecute As Boolean = True)
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Dim intCount As Integer
Set olRules = Application.Session.DefaultStore.GetRules
Set olRule = olRules.Item(RuleName)
If Enable Then olRule.Enabled = True Else olRule.Enabled = False
If blnExecute Then olRule.Execute ShowProgress:=True
olRules.Save
Set olRules = Nothing
Set olRule = Nothing
End Sub 'OnOffRunRule
As you can see I tried two different methods for dismissing the reminder ("Enable TEST" vs. "Disable TEST"). Both triggered the same runtime error.
Please ignore the Wait (5)
call, that just loops DoEvents until 5 seconds from the current time.
Apologies if duplicate, I spent several days searching and couldn't find the specific issue I'm having here. Most are Excel-specific.
CodePudding user response:
The error is MAPI_E_INVALID_PARAMETER
. Try to set Item.ReminderSet = false
instead of calling Reminder.Dismiss
.
You will probably be better off using Reminders.BeforeReminderShow
event (where Reminders
comes from Application.Reminders
) - you can set the Cancel
parameter passed to your event handler to true
.