VBOffice

Empty a Folder

Delete the content of a folder with a single click.

Last modified: 2017/01/10 | Accessed: 84.829  | #51
◀ Previous sample Next sample ▶

Content

Category-Manager Category-Manager
With Category-Manager you can group your Outlook categories, share them with other users, filter a folder by category, automatically categorize new emails, and more. You can use the Addin even for IMAP.

Empty the Entire Folder

If you want to empty a junk folder, there is a command in the context menu. However, for most of the other folders that command is not available. This script deletes the content of the current folder. You should get a prompt if the current folder is a search folder. Search folders are just views on folders and can display the content of all the mailbox. In this case you perhaps don´t really want to delete all the items.

Customize the ribbon to get the Empty Folder command for any folder, or call the macro by pressing alt+f8.

(This macro works for Outlook 2007 and higher.)

Here's another code for IMAP folder: Purge Deleted IMAP messages


tip  How to add macros to Outlook
Public Sub EmptyFolder()
  Dim Folder As Outlook.Folder
  Dim FolderDeleted As Outlook.Folder
  Dim Store As Outlook.Store
  Dim Items As Outlook.Items
  Dim Item As Object
  Dim Pa As Outlook.PropertyAccessor
  Dim i As Long, Count As Long
  Dim Delete As Boolean, IsSearchFolder As Boolean
  Dim Msg As String
  
  Set Folder = Application.ActiveExplorer.CurrentFolder
  Set Items = Folder.Items
  Count = Items.Count
  If Count = 0 Then Exit Sub
  
  Set Pa = Folder.PropertyAccessor
  IsSearchFolder = (Pa.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x36010003") = 2)
  If IsSearchFolder Then
    Msg = "Are you sure you want to delete " & Count & " items from a search folder?"
    If MsgBox(Msg, vbQuestion Or vbYesNoCancel Or vbDefaultButton2) <> vbYes Then Exit Sub
  Else
    Msg = "Delete " & Count & " items?"
    If MsgBox(Msg, vbQuestion Or vbYesNoCancel) <> vbYes Then Exit Sub
  End If
  
  Set Store = Folder.Store
  Set FolderDeleted = Store.GetDefaultFolder(olFolderDeletedItems)
  Delete = (Folder.EntryID = FolderDeleted.EntryID)
  
  If Delete Then
    For i = Count To 1 Step -1
      Items(i).Delete
    Next
  Else
    For i = Count To 1 Step -1
      Set Item = Items(i)
      Item.UnRead = False
      Item.Save
      Item.Move FolderDeleted
    Next
  End If
End Sub
ReplyAll ReplyAll
ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the e-mail.

Delete the Selected Items

This code actually does the same as the one above except it deletes only the selected items from the folder.

Public Sub EmptySelection()
  Dim Folder As Outlook.Folder
  Dim FolderDeleted As Outlook.Folder
  Dim Store As Outlook.Store
  Dim Item As Object
  Dim Selection As Outlook.Selection
  Dim Pa As Outlook.PropertyAccessor
  Dim i As Long, Count As Long
  Dim Delete As Boolean, IsSearchFolder As Boolean
  Dim Msg As String
  
  Set Folder = Application.ActiveExplorer.CurrentFolder
  Set Selection = Application.ActiveExplorer.Selection
  Count = Selection.Count
  If Count = 0 Then Exit Sub
  
  Set Pa = Folder.PropertyAccessor
  IsSearchFolder = (Pa.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x36010003") = 2)
  If IsSearchFolder Then
    Msg = "Are you sure you want to delete " & Count & " items from a search folder?"
    If MsgBox(Msg, vbQuestion Or vbYesNoCancel Or vbDefaultButton2) <> vbYes Then Exit Sub
  Else
    Msg = "Delete " & Count & " items?"
    If MsgBox(Msg, vbQuestion Or vbYesNoCancel) <> vbYes Then Exit Sub
  End If
  
  Set Store = Folder.Store
  Set FolderDeleted = Store.GetDefaultFolder(olFolderDeletedItems)
  Delete = (Folder.EntryID = FolderDeleted.EntryID)
  
  If Delete Then
    For i = Selection.Count To 1 Step -1
      Set Item = Selection(1)
      Item.Delete
    Next
  Else
    For i = Selection.Count To 1 Step -1
      Set Item = Selection(i)
      Item.UnRead = False
      Item.Save
      Item.Move FolderDeleted
    Next
  End If
End Sub
SAM SAM
Determine the "identity" of your emails. Set with SAM the sender and the folder folder for sent items with the help of rules.

Outlook 2003 And Older

This simple example doesn´t check for a search folder and works for Outlook 2003 and older, too.

Public Sub DeleteFolderContent()
  Dim Folder As Outlook.MAPIFolder
  Dim Items As Outlook.Items
  Dim Msg As String
  Dim i As Long

  Set Folder = Application.ActiveExplorer.CurrentFolder
  Set Items = Folder.Items

  Msg = Items.Count & " items in " & Folder.Name & ". Delete?"

  If MsgBox(Msg, vbYesNo) = vbYes Then
    For i = Items.Count To 1 Step -1
      Items.Remove i
    Next
  End If
End Sub
Category-Manager Category-Manager
With Category-Manager you can group your Outlook categories, share them with other users, filter a folder by category, automatically categorize new emails, and more. You can use the Addin even for IMAP.
email  Send a message