Get a folder based on its folder path

This example takes a folder path and obtains the associated folder.

Example

Note

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.

In the following code example, the GetKeyContacts method uses the GetRootFolder() property to obtain the folder path of the Contacts\Key Contacts folder. The GetFolder method is then called by using the FolderPath property as the argument. If GetFolder returns a folder, a message will appear saying the Key Contacts are found. The GetFolder method takes in a path to a folder and returns the correct Folder object. This is done by first splitting the FolderPath property into a string array and then using the array to find the correct Folder object starting from the top of the FolderPath property. If the specified folder is not found, GetFolder returns a null reference.

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void GetKeyContacts()
{
    string folderPath =
        Application.Session.
        DefaultStore.GetRootFolder().FolderPath
        + @"\Contacts\Key Contacts";
    Outlook.Folder folder = GetFolder(folderPath);
    if (folder != null)
    {
        //Work with folder here
        Debug.WriteLine("Found Key Contacts");
    }
}

// Returns Folder object based on folder path
private Outlook.Folder GetFolder(string folderPath)
{
    Outlook.Folder folder;
    string backslash = @"\";
    try
    {
        if (folderPath.StartsWith(@"\\"))
        {
            folderPath = folderPath.Remove(0, 2);
        }
        String[] folders =
            folderPath.Split(backslash.ToCharArray());
        folder =
            Application.Session.Folders[folders[0]]
            as Outlook.Folder;
        if (folder != null)
        {
            for (int i = 1; i <= folders.GetUpperBound(0); i++)
            {
                Outlook.Folders subFolders = folder.Folders;
                folder = subFolders[folders[i]]
                    as Outlook.Folder;
                if (folder == null)
                {
                    return null;
                }
            }
        }
        return folder;
    }
    catch { return null; }
}        

See also