2

I have a Macro that works inconsistently and it is unable to run without an error occuring at some stage. It works without any problem for days but then doesn't, seemingly without reason. I change nothing, do not do anything different and am curious as to the fickleness of VBA/macros only having been dealing with them for some weeks now. An error is generated in those instances that it doesn't run as expected.

Error :object doesn't support this property or method

--despite the option of caption being generated on the insertion of .

Sub SnoozedReminders()

    Dim oReminder As Reminder
    Dim oReminders  As Outlook.Reminders
    Dim RemItems As String

    Set oReminders = Outlook.Reminders
    For Each oReminder In oReminders

    If (oReminder.OriginalReminderDate <> oReminder.NextReminderDate) 
    Then
    RemItems = RemItems & oReminder.Caption & vbCrLf & _
    "Original Reminder time: " & oReminder.OriginalReminderDate & vbCrLf 
    & _
    "Snoozed to: " & oReminder.NextReminderDate & vbCrLf _
    & vbCrLf
    End If
    Next oReminder

    Set oMail = Application.CreateItem(olMailItem)
    oMail.Subject = "Generated on " & Now
    oMail.Body = RemItems
    oMail.Display
End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • 1
    "fickleness" typically indicates a wrong assumption made when writing your code, but it's difficult to say more without knowing exactly where your error occurs. – Tim Williams Sep 22 '21 at 18:19
  • 1
    ^^ Check to see when it errors if the object it is erroring out on is null, that's the most likely scenario with that particular error message if it normally works. – Warcupine Sep 22 '21 at 18:28

1 Answers1

0

In the code you are trying to set a type to the object:

Set oReminders = Outlook.Reminders

Instead, you need to use the following code which uses the Reminders property of the Outlook Application class:

Set oReminders = Application.Reminders

For example, the following code gets the Reminders collection and displays the captions of all reminders in the collection. If no current reminders are available, a message is displayed to the user.

Sub ViewReminderInfo() 
 'Lists reminder caption information 
 Dim objRem As Outlook.Reminder 
 Dim objRems As Outlook.Reminders 
 Dim strTitle As String 
 Dim strReport As String 
 
 Set objRems = Application.Reminders 
 strTitle = "Current Reminders:" 
 strReport = "" 
 'If there are reminders, display message 
 If Application.Reminders.Count <> 0 Then 
   For Each objRem In objRems 
     'Add information to string 
     strReport = strReport & objRem.Caption & vbCr 
   Next objRem 
   'Display report in dialog 
   MsgBox strTitle & vbCr & vbCr & strReport 
 Else 
   MsgBox "There are no reminders in the collection." 
 End If 
 
End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45