• Outlook User
  • Exchange Admin
  • Office 365
  • Outlook Developer
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
    • Common Problems
    • Outlook BCM
    • Utilities & Addins

Create Outlook Folders from a List of Folder Names

Slipstick Systems

› Developer › Code Samples › Create Outlook Folders from a List of Folder Names

Last reviewed on July 2, 2021     53 Comments

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.

create new folders 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.)

Create subfolders

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:

  1. Right click on Project1 and choose Insert > Module
  2. 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

Create Outlook Folders from a List of Folder Names was last modified: July 2nd, 2021 by Diane Poremsky
  • Twitter
  • Facebook
  • LinkedIn
  • Reddit
  • Print

Related Posts:

  • How to Create Messages Using Data in an Excel File
  • Import meetings from a CSV or XLSX file
  • Copy data from Outlook email tables to Excel
  • Create new Outlook folders using PowerShell

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Subscribe
Notify of
53 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

malcolm larkin (@guest_217766)
March 15, 2021 3:33 pm
#217766

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

1
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  malcolm larkin
March 16, 2021 12:25 am
#217772

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.

0
0
Reply
Laura (@guest_219936)
Reply to  Diane Poremsky
December 19, 2022 11:30 am
#219936

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.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Laura
December 19, 2022 2:50 pm
#219937

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.

0
0
Reply
malcolm larkin (@guest_217765)
March 15, 2021 3:29 pm
#217765

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

0
0
Reply
Hardip Dabhi (@guest_217642)
March 2, 2021 8:04 am
#217642

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

error.png
0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Hardip Dabhi
March 2, 2021 10:05 am
#217645

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.

  1. Go to Tools, References menu.
  2. Locate the object library in the list and add a check mark to it. 

Are you adding the code to Outlook's VB Editor or Excel's ?

0
0
Reply
Hardip Dabhi (@guest_217654)
Reply to  Diane Poremsky
March 3, 2021 5:09 am
#217654

Hi Diane, I am adding this code in the Excel editor.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Hardip Dabhi
March 3, 2021 8:10 am
#217655

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.

0
0
Reply
Hardip Dabhi (@guest_217664)
Reply to  Diane Poremsky
March 4, 2021 12:12 am
#217664

Diane, I run on outlook macro and its works...
Thanks a lot man.

0
0
Reply
Karin (@guest_217605)
February 22, 2021 3:20 am
#217605

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.

0
0
Reply
Andrew (@guest_215647)
July 24, 2020 9:58 am
#215647

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

0
0
Reply
Ali (@guest_214549)
January 3, 2020 5:12 am
#214549

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

0
0
Reply
Samuele (@guest_214451)
December 9, 2019 10:21 am
#214451

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?

0
0
Reply
jkspecial (@guest_213622)
July 18, 2019 5:06 pm
#213622

Whenever I try to run this, I get a runtime error:

"Run-time error '-2147221233 (800401f)':

The attempted operation failed. An object could not be found."

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  jkspecial
July 18, 2019 11:27 pm
#213623

Does It stop on a specific line? The error is because an object doesn't exist - this is most likely a folder, but could be any object.

0
0
Reply
jkspecial (@guest_213630)
Reply to  Diane Poremsky
July 19, 2019 12:31 pm
#213630

Hi Diane,

Thanks for the quick response! Wasn't sure how long that would take...

It highlights this line when I attempt to Debug - Set objParentFolder = objParentFolder.Folders(parentname)

0
0
Reply
Dan (@guest_214123)
Reply to  jkspecial
October 16, 2019 5:31 pm
#214123

Hello, I ran into the same problem, the problem is one extra line of code trying to reference the parent folder before it checks if there is a parent folder and subsequently creates one.
The exact line of code is: ((( 'select starting parent )))
--------------------------------------------------------------
iRow = 2

'select starting parent
Set objParentFolder = Application.ActiveExplorer.CurrentFolder
---------------------------------------
delete ((( 'select starting parent )))
Should fix the error.
If it does not make sure your excel list is saved as CSV as well as trust setting to allow the macro.

0
0
Reply
Michael (@guest_214345)
Reply to  Dan
November 21, 2019 3:20 pm
#214345

I solved this error by going a parent folder higher than the Inbox. The first parent folder in my Outlook was actually my emaill address name and then the Inbox. When I had Inbox as the first folder I received the above error. Once I changed the first parent folder to my email address, then Inbox and then my new folders, the macro worked perfectly.

Thanks for sharing!

1
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 28 Issue 11

Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Subscribe to Exchange Messaging Outlook






Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • WeekMonthAll
  • Adjusting Outlook's Zoom Setting in Email
  • How to Remove the Primary Account from Outlook
  • Cannot add Recipients in To, CC, BCC fields on MacOS
  • Move an Outlook Personal Folders .pst File
  • Save Sent Items in Shared Mailbox Sent Items folder
  • Create rules that apply to an entire domain
  • Outlook's Left Navigation Bar
  • Use PowerShell to get a list of Distribution Group members
  • View Shared Calendar Category Colors
  • Remove a password from an Outlook *.pst File
  • Cannot add Recipients in To, CC, BCC fields on MacOS
  • Change Appointment Reminder Sounds
  • Messages appear duplicated in message list
  • Reset the New Outlook Profile
  • Delete Old Calendar Events using VBA
  • Use PowerShell or VBA to get Outlook folder creation date
  • Outlook's Left Navigation Bar
  • Contact's Display Bug
  • Use PowerShell to get a list of Distribution Group members
  • Edit Outlook’s Attach File list
Ajax spinner

Newest Code Samples

Delete Old Calendar Events using VBA

Use PowerShell or VBA to get Outlook folder creation date

Rename Outlook Attachments

Format Images in Outlook Email

Set Outlook Online or Offline using VBScript or PowerShell

List snoozed reminders and snooze-times

Search your Contacts using PowerShell

Filter mail when you are not the only recipient

Add Contact Information to a Task

Process Mail that was Auto Forwarded by a Rule

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in Outlook for Windows.

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




Windows 10 Issues

  • iCloud, Outlook 2016, and Windows 10
  • Outlook Links Won’t Open In Windows 10
  • Outlook can’t send mail in Windows 10: error Ox800CCC13
  • Missing Outlook data files after upgrading Windows?

Outlook Top Issues

  • The Windows Store Outlook App
  • The Signature or Stationery and Fonts button doesn’t work
  • Outlook’s New Account Setup Wizard
  • Outlook 2016: No BCM
  • Exchange Account Set-up Missing in Outlook 2016

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

Outlook-tips.net Samples

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

MSDN Outlook Dev Forum

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Contact Tools

Data Entry and Updating

Duplicate Checkers

Phone Number Updates

Contact Management Tools

Diane Poremsky [Outlook MVP]

Make a donation

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Outlook Suggestion Box (UserVoice)

Slipstick Support Services

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Advertise | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2023 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment