This Outlook macro creates subfolders under the currently selected folder.
Use PowerShell to add or delete folders, using a list of folders in a text file: Create new Outlook folders using PowerShell
To use, create an Excel file with the desired folder names in one column with a header row. The folder names will begin with row 2 (cell A2). You can create the file in Notepad and save it with the CSV extension.
Use Set objParentFolder = objNewFolder to create nested folders.
Option Explicit Public Sub MoveSelectedMessages() Dim objParentFolder As Outlook.Folder ' parent Dim newFolderName 'As String Dim strFilepath Dim xlApp As Object 'Excel.Application Dim xlWkb As Object ' As Workbook Dim xlSht As Object ' As Worksheet Dim rng As Object 'Range Set xlApp = CreateObject("Excel.Application") strFilepath = xlApp.GetOpenFilename If strFilepath = False Then xlApp.Quit Set xlApp = Nothing Exit Sub End If Set xlWkb = xlApp.Workbooks.Open(strFilepath) Set xlSht = xlWkb.Worksheets(1) Dim iRow As Integer iRow = 2 Set objParentFolder = Application.ActiveExplorer.CurrentFolder While xlSht.Cells(iRow, 1) <> "" newFolderName = xlSht.Cells(iRow, 1) On Error Resume Next Dim objNewFolder As Outlook.Folder Set objNewFolder = objParentFolder.Folders(newFolderName) If objNewFolder Is Nothing Then Set objNewFolder = objParentFolder.Folders.Add(newFolderName) End If iRow = iRow + 1 ' make new folder the parent ' Set objParentFolder = objNewFolder Set objNewFolder = Nothing Wend xlWkb.Close xlApp.Quit Set xlWkb = Nothing Set xlApp = Nothing Set objParentFolder = Nothing End Sub
Create subfolders at multiple levels
This code snippet uses the folder name in Column 1 to set the parent folder, with the new folder name in Column 2. Note that the parent folder needs to be the last one created (or the Inbox).
However, because the macro checks for the existence of the folder and creates it only if it doesn't exist, you can walk the folders to create deep subfolders. (Note: I never recommend deeply nested subfolders, it's too easy to forget where they are.)
A complete copy of this macro is here
'select starting parent Set objParentFolder = Application.ActiveExplorer.CurrentFolder Dim parentname While xlSht.Cells(iRow, 1) <> "" parentName = xlSht.Cells(iRow, 1) newFolderName = xlSht.Cells(iRow, 2) If parentName = "Inbox" Then Set objParentFolder = Session.GetDefaultFolder(olFolderInbox) Else Set objParentFolder = objParentFolder.Folders(parentName) End If On Error Resume Next Dim objNewFolder As Outlook.Folder Set objNewFolder = objParentFolder.Folders(newFolderName)
How to use macros
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s 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.
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
More Information
Copy Folder Structure from Explorer into Outlook 2013
Hello Diane -
I'm trying to get this to work in an Archive folder, not in my Inbox. I move project related files from my Inbox to the Archive, organized by Customer > Project Number.
I've read through all the comments below and can't get the multi-level folders to set up properly. I'm using this code. The main folders are numbered [00] Proposal, [01] Project Plan etc up to [05] Deliverables. Each main folder has sub folders. However, when I run the code, all the main folders are nested within the [00] folder. Attached are screenshots of my input file and the results are attached.
Is there something I need to change if I'm doing this in the Archive folder, instead of the inbox?
I am creating the Project folder under the customer, then selecting it as the starting folder before running the macro.
Thanks!
Bob
Hi,
I have managed to get the macro up and running, the problem i have is there are 4 email accounts in my outlook - i would like to run this on inbox3. Any ideas would be greatly apprecited
This line uses the selected folder -
Set objParentFolder = Application.ActiveExplorer.CurrentFolder
if you want to run it on the entire mailbox, select the root folder - which is usually the email address. to add the folders as subfolders of the inbox, select the inbox.
Is this the same for a shared inbox? I can get the code to work perfectly to create a two level folder structure in my own inbox, but I need to direct this to a shared inbox instead.
Because it uses the current folder - Set objParentFolder = Application.ActiveExplorer.CurrentFolder - it should work find in a shared mailbox. Select the folder where you want them created - if at the same level as the inbox, select the shared mailbox name.
Hi there, the code given works brilliantly, i have however hit a barrier - I have multiple inboxes in my outlook "account1" is my default account, then there are 3 others "account2", "account3" and "account4". I would like to run this on account3 but i can not get it to work.
Any help would be greatly appreciated
Hi Developer,
I have tried to create multiple folders in the outlook 365 app but unfortunately getting the below error.
Compile error:
User-defined type not defined
That error usually means you need to set a reference to another object model - Excel in this case - but the code as written shouldn't require that.
Are you adding the code to Outlook's VB Editor or Excel's ?
Hi Diane, I am adding this code in the Excel editor.
As written, its an outlook macro. Switching it to an Excel macro wouldn't be difficult - mostly just the DIM lines need to be changed and outlook declared instead of Excel.
The Excel macro at Create Appointments Using Spreadsheet Data (slipstick.com) is an example.
Diane, I run on outlook macro and its works...
Thanks a lot man.
Hi there!
This works brilliantly - I used to have the macro, but forgot to back it up when I had to format my PC.
Unfortunately, the link for the complete macro links to the moving the folders macro and not creating the folders macro.
Hi there!
I'm very much of a rookie to all of this.
I've got my spreadsheet with the folders i want to create and copied and pasted the above macro into the Vba thing.
Do i need to change the parts in green to match my spreadsheet?
Or do i need to import my spreadsheet somehow once i've saved the macro?
Sorry for all the basic questions!
Any help kindly appreciated
Hi there! I've spent like 1 hour ro figure out how to get away from the:
"Run-time error '-2147221233 (800401f)':
The attempted operation failed. An object could not be found."
But I really cannot understand, I've tried to see somewhere on Google, but nothing useful
I've 3 emails boxes in the Outlook program, I think that's the problem, but can't figure out how to solve it.
Thank you
Is it possible to create a script based on the same excel file to move all emails containing the subfolder name to the created subfolders? Can you make a post about it please?