Macro to create outlook folders

shyy

Well-known Member
Joined
Nov 6, 2008
Messages
1,484
Hi guys,

Can someone make a macro to create folders using the description in cells A1:A10. Every year I have to rollover bunch of folders.

Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Something simple like the code below can be adapted to accomplish what you're looking for. Note the getObject() function requires Outlook to be open.

Code:
Sub CreateOutlookFolders()

Dim outApp As Object 'Outlook.Application
Dim outFldr As Object 'Outlook.Folder
Dim rng As Range, c As Range


Set outApp = GetObject(, "Outlook.Application")
Set outFldr = outApp.Session.GetDefaultFolder(6)
'6 = olFolderInbox - change this to reference the folder you want to add your new folders to
Set rng = Range("A1:A10") 'Range of Folder Names


On Error Resume Next 'Ensures the code doesn't fall over if any of the cells in your range are blank
    For Each c In rng
        outFldr.Folders.Add c
    Next
On Error GoTo 0


Set rng = Nothing
Set outFldr = Nothing
Set outApp = Nothing


End Sub

Hope this helps

Simon
 
Upvote 0
Thank You, it worked!

Question, when you say reference do you mean to actually type in the name of the folder? I do have couple main folders and sub folders. I also have multiple admin boxes. Ex: outApp.Session.GetDefaultFolder("test folder")
 
Upvote 0
You may find it easier to understand the code when it is using early binding. In the example below the code is using a sub folder of the inbox called "test folder". You can move as far down the tree as you need by adding on further .Folders("folder name")

Code:
Sub CreateOutlookFolders()

Dim outApp As Outlook.Application
Dim outFldr As Outlook.Folder
Dim rng As Range, c As Range


Set outApp = Outlook.Application
'Set outFldr = outApp.Session.GetDefaultFolder(olFolderInbox)
Set outFldr = outApp.Session.GetDefaultFolder(olFolderInbox).Folders("test folder")
Set rng = Range("A1:A10")


On Error Resume Next
    For Each c In rng
        outFldr.Folders.Add c
    Next
On Error GoTo 0


Set rng = Nothing
Set outFldr = Nothing
Set outApp = Nothing


End Sub
 
Upvote 0
Apologies, I rather unhelpfully forgot to mention that with Early Binding you need to set a reference to the Oulook Object Library. You can do this by clicking on Tools\References in the Visual Basic Editor and selecting "Microsoft Outlook X.0 Object Library" where X represents the version of Outlook installed. The code should then run
 
Upvote 0
Appreciate this is an old thread, but is it possible to get an update with nested folders i.e. excel Columns A, B (maybe C), say 2 to (max) 3 deep?
Thanks k.
 
Upvote 0

Forum statistics

Threads
1,223,727
Messages
6,174,148
Members
452,547
Latest member
Schilling

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top