Look for and create folders programmatically in Outlook

December 30, 2008JP8 CommentsRate This ArticlenewLinks 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.

Function CheckForFolder(strFolder As String) As Boolean
' 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:

If CheckForFolder("My Folder") Then
  ' 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.

Function CreateSubFolder(strFolder As String) As Outlook.MAPIFolder
' 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:

Dim MyFolder As Outlook.MAPIFolder
If CheckForFolder("My Folder") = False Then ' Folder doesn't exist
 Set MyFolder = CreateSubFolder("My Folder")
End If

About JP
I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

8 Response(s) to Look for and create folders programmatically in Outlook ↓

  1. Reid Parker says:

    That was EXACTLY the piece of code I was looking for. Thanks very much for posting it!

  2. JP says:

    Glad to help, Reid. 8)

  3. Gary says:

    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?

  4. Gary says:

    Correct, but is there a way to do that in VBA?
    I would guess something like

    Function CreateSubFolder(strFolder As String, blnForSent as Boolean) As Outlook.MAPIFolder
    ' 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
    • diya says:

      hey hi i am very new, wanted to add contacts in my new folder but unable to do so,

      please help me !

      • JP says:

        The code I posted will only create Mail-type folders. You would need to change this line:

        Set olInbox = olNS.GetDefaultFolder(olFolderContacts)

        Then in my sample code, "MyFolder" would point to your new Contacts folder:

        Set MyFolder = CreateSubFolder("My Folder")

        All you would do then is use the Folder.Items.Add method to add a new contact to the new folder:

        MyFolder.Items.Add
  5. JP says:

    I'm still trying to nail down the exact syntax, but it would be something like

    Dim vw As View
    Set vw = CreateSubFolder.Views.Add("My New View", olTableView, olViewSaveOptionAllFoldersOfType)

    Actually setting the folder to that view seems to be a moving target.

Speak Your Mind

Tell us what you're thinking...

Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].




Site last updated September 2, 2010 @ 7:03 pm