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
Delete Folders
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
Hello Diane! It's been many years since we last spoke! I hope you are doing well.
I happened to find your macro after pulling the same bone-headed mistake as many of us and accidentally deleting a large number of folders and I'd like restore the folders as well as the original contents from the Deleted Items to a mail folder of my choosing. Will this macro, as written do that? Or will it require a bit of modification? It's been so long since I've done VBA especially since PowerShell has come along, so I am rusty to say the least. However, if I can confirm this will do what I'm seeking out of the box, then this is my solution.
Of course I'll test it before running it on everything in the entire folder once again, but I'm a bit gun-shy to run another macro, especially on Deleted Items and lose everything entirely. Thank you!
Hello Diane.
I hope that all is well with you in these trying times. First of I must say that I enjoy this site very much. I am constantly on here learning to script/code.
I would like some help in creating a VB Script for Outlook similar to what you have above. Basically, I would like to have a script that will move folders from my outlook mailbox into my personal archive folder (Online Archive) within my Outlook to free up space in my mailbox. Any help will greatly be appreciated
is there an example of moving the currently selected folder to a PST?
I have a sample here:
https://www.slipstick.com/developer/code-samples/move-outlook-folders-vba/