Tagged with " outlook"
Dec 23, 2008 - tech, vba    6 Comments

Outlook VBA Code to Empty Deleted Items Folders

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.