Remove Items From the Deleted Items...

  • Thread starter Thread starter Marty
  • Start date Start date
M

Marty

I am new to VBA in Outlook. I do know VBA for Access and
Excel very well but I cannot seem to make the transition
to Outlook. Does anyone have any VBA to remove items from
the "Deleted Items" folder using, say, a for... next
loop? I'm getting 400 e-mails a day from bogus microsoft
alerts. I was able to create rules to delete every one of
them, but I do not have permissions to use
the "permenately delete" rule, so everything goes in
the "Deleted Items" folder. I was hoping to use VBA to
clean this folder on a timed interval of maybe every 5
minutes or so.

Thank you very much for your consideration,

Marty
 
Hi Marty,

same here. I'm fairly skilled with Excel & VBA, and also wrote quite a bit
of VBA code for Access a couple of years ago, but I'm not getting there with
Outlook... Anyways, here's a code that works with a limitation: "Mail
Messages" only. If there are any meeting requests, for example, the macro
fails. If someone knows a solution to my problem, let me know. Thanks.

Regards,
Jouni
Finland

Sub RemoveDeletedMailMessages()
Dim objOutlook As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objItems As Outlook.Items
Dim objMail As MailItem
' Dim lngRetVal As Long

' initialize
'check to see if Outlook is already open........... my outlook's always
open when macros are run.
' lngRetVal = FindWindowByClass("rctrl_renwnd32", 0&)

'if Outlook is open, get the existing object, otherwise create new
Object
' If lngRetVal <> 0 Then
Set objOutlook = GetObject(, "outlook.application")
' Else
' Set objOutlook = CreateObject("outlook.application")
' End If

'create objects to find a mail item
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objItems = objNameSpace.GetDefaultFolder(olFolderDeletedItems).Items

On Error Resume Next
For Each objMail In objItems
objMail.Delete
Next objMail

On Error GoTo 0

End Sub
 
You can't know that every item is a MailItem. Therefore, declare your basic item object as Object. If you need to use item-specific properties or methods, check the class property first.

--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
 
Back
Top