Look for and create folders programmatically in Outlook
December 30, 2008 • JP • 8 Comments • Rate This Article
• Links to this article
If you are writing VBA code for Outlook, you might have a need to check for an existing folder, and create it if it doesn't exist.
Here are two functions that can assist. I wrote one that checks for an existing folder, and returns TRUE if it exists. It assumes a specific folder hierarchy, so you'll need to adjust it if the folders you are working with are on a different level. Works in Outlook but can be adapted for Excel use as well.
' looks for subfolder of specified folder, returns TRUE if folder exists.
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olInbox As Outlook.MAPIFolder
Dim FolderToCheck As Outlook.MAPIFolder
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox = olNS.GetDefaultFolder(olFolderInbox)
' try to set an object reference to specified folder
On Error Resume Next
Set FolderToCheck = olInbox.Folders(strFolder)
On Error Goto 0
If Not FolderTocheck Is Nothing Then
CheckForFolder = True
End If
ExitProc:
Set FolderToCheck = Nothing
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function
To use this in your code:
' your code here
End If
This code will look for Inbox\My Folder and return TRUE if it exists.
The second function will actually create the sub folder. It is written so that it should only be called if the folder doesn't exist, so it should be used in tandem with the above function.
' assumes folder doesn't exist, so only call if calling sub knows that
' the folder doesn't exist; returns a folder object to calling sub
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olInbox As Outlook.MAPIFolder
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox = olNS.GetDefaultFolder(olFolderInbox)
Set CreateSubFolder = olInbox.Folders.Add(strFolder)
ExitProc:
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function
This function returns an object reference to the MAPIFolder Object created by the code. Usage would be:
If CheckForFolder("My Folder") = False Then ' Folder doesn't exist
Set MyFolder = CreateSubFolder("My Folder")
End If
Previous Post: Update Outlook Task Reminder Dates from Excel
Next Post: Happy New Year to all!




That was EXACTLY the piece of code I was looking for. Thanks very much for posting it!
Glad to help, Reid.
That's fine if you're going to use it to keep mail items that you recieve. What if you use it to keep mail items you've sent? When you click on the folder, it has 'From' and 'Recieved' fields. How do you modify the folder after adding it so 'To' and 'Sent' fields are shown instead?
Gary,
Right-click on one of the fields and select "Field Chooser", you can add those fields back in.
Correct, but is there a way to do that in VBA?
I would guess something like
' assumes folder doesn't exist, so only call if calling sub knows that
' the folder doesn't exist; returns a folder object to calling sub
' Have calling sub set blnForSent = True for folder to display 'To' and 'Sent' fields
' blnForSent = False for folder to display 'From' and 'Received' fields
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olInbox As Outlook.MAPIFolder
Dim olFldr As Outlook.MAPIFolder
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox = olNS.GetDefaultFolder(olFolderInbox)
Set olFldr = olInbox.Folders.Add(strFolder)
If blnForSent Then
'olFldr.CurrentView.???
End If
Set CreateSubFolder = olFldr
ExitProc:
Set olFldr = Nothing
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function
hey hi i am very new, wanted to add contacts in my new folder but unable to do so,
please help me !
The code I posted will only create Mail-type folders. You would need to change this line:
Then in my sample code, "MyFolder" would point to your new Contacts folder:
All you would do then is use the Folder.Items.Add method to add a new contact to the new folder:
I'm still trying to nail down the exact syntax, but it would be something like
Set vw = CreateSubFolder.Views.Add("My New View", olTableView, olViewSaveOptionAllFoldersOfType)
Actually setting the folder to that view seems to be a moving target.