A client creates folders for each project he works on. When he's finished with the project, he moves it to an 'Old Projects' folder by selecting the folder and using the Move folder command to open the Folder picker to select the old projects folder. It's usually easier to drag the folder to its new parent folder, but his folder tree is large, making dragging unwieldy.
He wondered if a macro could handle the move. Yes! The macro can pick up the current folder and move it to a specified folder in the current data file, or in another data file. Note: This macro may not work with folders in IMAP account, especially if the folder contains email. It will move the folder, but the IMAP server rejects the move and the folder reappears in its original position.
In this example, the 'Old Projects' folder is at the same level as the Inbox and other default folders.
Sub Movefolders() Dim OutApp As Outlook.Application Dim oNS As Outlook.NameSpace Dim objInboxFolder As Outlook.MAPIFolder Dim objDestFolder As Outlook.MAPIFolder Dim curFolder As Outlook.MAPIFolder Set OutApp = Application Set oNS = OutApp.GetNamespace("MAPI") 'use the selected folder Set curFolder = OutApp.ActiveExplorer.CurrentFolder Set objInboxFolder = oNS.GetDefaultFolder(olFolderInbox) Set objDestFolder = objInboxFolder.Parent.Folders("Old Projects") ' move folder curFolder.MoveTo objDestFolder ' copy folder 'curFolder.CopyTo objDestFolder End Sub
With just a small edit, you can use the macro to delete folders. The selected folder will be moved into the Deleted Items folder; by repeating the Delete line, it will be permanently deleted.
Note: Outlook's default folders cannot be deleted.
Sub DeleteFolders() Dim oOutlook As Outlook.Application Dim objNamespace As Outlook.NameSpace Dim curFolder As Outlook.MAPIFolder Set oOutlook = Application Set objNamespace = oOutlook.GetNamespace("MAPI") Set curFolder = oOutlook.ActiveExplorer.CurrentFolder ' move to Deleted Items folder curFolder.Delete ' delete it from deleted items curFolder.Delete End Sub
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Macros that run when Outlook starts or automatically need to be in ThisOutlookSession, all other macros should be put in a module, but most will also work if placed in ThisOutlookSession. (It's generally recommended to keep only the automatic macros in ThisOutlookSession and use modules for all other macros.) The instructions are below.
The macros on this page should be placed in a module.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor