Sauron
Sauron

Reputation: 6657

Auto Delete All Outlook Calendar Items in Deleted Folder

I am writing a simply script to delete all calendar objects within the Deleted Items folder. such calendar objects include meeting confirmations, declines...etc.

At present I have below, does not delete anything, even though checking the deleted items folder has many calendar confirmations, declines..etc:

    Dim oDeletedItems As Outlook.Folder
    Dim obj As Outlook.MailItem

    Dim i As Integer

    'Obtain a reference to deleted items folder
    Set oDeletedItems = Application.Session.GetDefaultFolder(olFolderDeletedItems)
    For i = oDeletedItems.Items.Count - 1 To 1 Step -1
    ' Delete all appointment items
     If oDeletedItems.Items(i).Class = AppointmentItem Then

         'Debug.Print obj.Subject
          oDeletedItems.Items.Item(i).Delete

     End If
    Next

Upvotes: 0

Views: 690

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66245

Firstly, do not use multiple dot notation (oDeletedItems.Items(i).Class) - cache the Items collection before entering the loop.

Secondly, confirmations, declines, etc. are meeting items, not appointments.

Thirdly, Class property returns one of the OlObjectClass enum values. AppointmentItem is an item type (interface).

You need to use 53 (olMeetingRequest) or 26 (olAppointment).

Fourthly, start the loop from Items.Count, not Items.Count-1.

set items = oDeletedItems.Items
For i = items.Count To 1 Step -1
    ' Delete all appointment items
     set item = items.Item(I) 
     itemClass = item.Class
     If (itemClass= olAppointment) or (itemClass= olMeetingRequest) Then
          'Debug.Print obj.Subject
          item.Delete
     End If
    Next

Upvotes: 1

Related Questions