Outlook VBA Code to Empty Deleted Items Folders December 23, 2008

I’ve been working and playing around quite a bit with MS Excel and MS Outlook VBA code and have written quite a few tools / add-ins / functionality / macro code that automates several manual processes. Will try and add as many of these up as possible over time, starting today.

I have multiple personal folders (PSTs) in my MS Outlook profile and have rules set up to automatically move the emails to appropriate folders. When I delete an email, it is sent to the corresponding personal folder’s Deleted Items folder.

Now, MS Outlook has an ‘Empty “Deleted Items” Folder’ menu option, but that only empties the Deleted Items folder in the default mailbox and not the other personal folders. To empty these folders, one manually needs to right-click on each Deleted Items folder and click the ‘Empty “Deleted Items” Folder’ option.

Therefore I wrote this MS Outlook VBA macro code that is used to empty all the Deleted Items folders with one click.

You can download the code here.

To use this in your MS Outlook: Press ALT-F11 in MS Outlook to open the Microsoft Visual Basic editor. Macros need to be enabled and if Macros have not been enabled when you pressed ALT-F11, you will get a prompt to enable the same.

In the Project explorer on the left, right-click on Modules and Insert a new Module. Open the  text file that you downloaded above and copy-paste the code in the module.

To customise the code for your requirements, firstly figure out how many Personal Folders you have and their names.

Add the following code as many times as there are Personal Folders in your outlook profile, and replace “Friends” with your Personal Folder name:

‘***Empty Personal Folders Deleted Items Folder
‘Change the current folder to the target personal folder / pst name
Set personalFolder = mNameSpace.Folders(“Friends”) ‘Friends is the name of the Personal Folder
Set objExpl.CurrentFolder = personalFolder.Folders(“Deleted Items”)
‘Select the target folder
objExpl.SelectFolder personalFolder.Folders(“Deleted Items”)
‘Get the Empty Deleted Items command = UID 1671
Set objCBB = objExpl.CommandBars.FindControl(, 1671)
‘Execute the command
objCBB.Execute

At present the code has details of two Personal Folders (Friends and Family)

To run the above code you can press ALT-F8 (or go to Tools -> Macro -> Macros) and then select EmptyDeletedItems and click Run.

You may also add a shortcut button to the Toolbar, so you can run the macro in one click. For this right-click on the Toolbar and click on Customize. Click on the Commands tab and then select Macros as the Category and then drag and drop the EmptyDeletedItems macro command to a location on the Toolbar.

Running the macro now empties all the Deleted Items folders, in all the Personal Folders and the default Mailbox.

Download the code here.

Article printed from Sabarish: http://sabarish.net/blog
URL to article: http://sabarish.net/blog/archives/437

Tags
Conversation

Comments
Trackback

    5 Responses to “Outlook VBA Code to Empty Deleted Items Folders”

  1. Automatically empty 2 or more "Deleted Items" folders - Page 2 | keyongtech January 18th, 2009 at 10:23 pm | Permalink

    [...] that links to that macro. I’ve uploaded the macro along with instructions on how to use it here – http://sabarish.net/blog/archives/437 Do let me know if that solves your problem. Cheers, Sabarish "Tyler G." wrote: > I [...]

  2. Searcher September 23rd, 2009 at 8:18 pm | Permalink

    Hi,

    I came across your article when I decided to create separate PST files for each email account and realized there isn’t a logical way to remove deleted emails. Thanks to your code I managed to deal with the problem quite quickly..

    but I’m wondering if you actually tested your code and it was working for you?

    You are calling UID 1671 which is the main Empty “Deleted Items” Folder and as such it’s emptying only the default account, no matter if you selected another one.

    So the solution is to enter the respective deleted items folder, loop through it and remove all items.

    I thought I’d let you know ;)

    -src-

  3. sabarishr September 23rd, 2009 at 9:07 pm | Permalink

    Hey Searcher!

    Thanks for bringing that to my notice! I had actually uploaded the wrong code. For representational purposes I had copied and pasted that repeating section from the main deleted items folder and forgot to change the values. I’ve uploaded the new version now.

    You don’t need to loop through all files – you can do a select all (ID 756) and delete (ID 478) directly.

    Cheers,
    Sab

  4. Jay February 7th, 2010 at 6:40 am | Permalink

    I am using your code, but I get the message pop-up asking if I am sure I want to delete the items. How do I get rid of that pop-up and just have the items deleted?

  5. Jay February 7th, 2010 at 6:43 am | Permalink

    Nevermind, I found it in the Outlook Program.

    Tools>Options>Other>Advanced Options>

    Uncheck “Warn before permenantly deleting items.”

    Best


Leave a Reply

This entry was posted on Tuesday, December 23rd, 2008 at 3:51 pm. You can follow any responses to this entry through the RSS 2.0 feed. If you're wondering how to get your own icon next to your comment, go visit gravatar.com and get yourself hooked up.
Sabarish is proudly powered by Wordpress 2.9.2 and Squible Beta 2.
All content is copyrighted by its author[s].