I rarely write code and have little understanding of the Outlook objects. The following loops through specific folders and deletes messages based on conditions.
I am not getting any error messages, it just does not delete the message when I uncomment the conditional expression for the MeetingItem and ApppointmentItem and the condition has been met.
I do not think I have it properly Set = something
to access .Start of the MeetingItem
You don't know what you don't know...
I am not getting any error messages, it just does not delete the message when I uncomment the conditional expression for the MeetingItem and ApppointmentItem and the condition has been met.
I do not think I have it properly Set = something
to access .Start of the MeetingItem
Code:
Public Sub DeleteUnwantedMail()
'This script loops through Delete Folder and deletes unwanted items
Dim ApptDate As Outlook.AppointmentItem
Dim MeetDate As Outlook.MeetingItem
Dim MeetItem As MeetingItem
Dim dtmToday As Date
Dim olItem As Outlook.Items
Dim intDays As Long
Dim x As Integer
Dim y As Integer
On Error GoTo errorhandler:
dtmToday = Date 'Assigns todays date to variable
Dim arrFolder(1 To 1) As Integer
arrFolder(1) = 23 'Junk Folder -
arrFolder(2) = 5 'Sent Items Folder
arrFolder(3) = 3 'Deleted Items Folder
'Loop through specific folders
For x = LBound(arrFolder) To UBound(arrFolder)
'Set folder to be current folder
Set olItem = Application.GetNamespace("MAPI").GetDefaultFolder(arrFolder(x)).Items
'Sort the folder descending order
olItem.Sort "[Received]", True
'Loops through each message type, determines message type and delete if criteria met
For y = olItem.Count To 1 Step -1 'Set up loop
'reset intDays
' intDays = 0
Select Case TypeName(olItem(y))
Case "MailItem"
If olItem(y).UnRead Then
olItem(y).Delete
Else
intDays = DateDiff("d", olItem(y).ReceivedTime, dtmToday)
If intDays >= 90 Then
olItem(y).Delete
End If
End If
Case "SharingItem"
If olItem(y).UnRead Then
olItem(y).Delete
End If
Case "MeetingItem"
'If olItem(y).Start < dtmToday Then
olItem(y).Delete
'End If
Case "AppointmentItem"
If olItem(y).Start < dtmToday Then
olItem(y).Delete
End If
Case Else
'For now skip over any message items not in case list
End Select
Next y
Next x
' tidy up
Set olItem = Nothing
Exit Sub
errorhandler:
If Err.Number = -2146893792 Then 'Encrypted message, cannot delete encrypted message with vba
Resume Next
'ElseIf Err.Number = ??? Then
' Resume Next
'Else
' MsgBox "Error: " & Err.Number & " " & Err.Description, vbOKOnly, "Error Not Handled"
' Resume Next
End If
End Sub
You don't know what you don't know...