How to: Programmatically Retrieve a Folder by Name

This example gets a reference to a named custom folder and then displays the contents of the folder.

Applies to: The information in this topic applies to application-level projects for Outlook 2013 and Outlook 2010. For more information, see Features Available by Office Application and Project Type.

Example

Private Sub SetCurrentFolder()
    Dim folderName As String = "TestFolder" 
    Dim inBox As Outlook.MAPIFolder = Me.Application.ActiveExplorer().Session. _
        GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
    Try 
        Me.Application.ActiveExplorer().CurrentFolder = inBox.Folders(folderName)
        Me.Application.ActiveExplorer().CurrentFolder.Display()
    Catch
        MsgBox("There is no folder named " & folderName & _
            ".", "Find Folder Name")
    End Try 
End Sub
private void SetCurrentFolder()
{
    string folderName = "TestFolder";
    Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)
        this.Application.ActiveExplorer().Session.GetDefaultFolder
        (Outlook.OlDefaultFolders.olFolderInbox);
    try
    {
        this.Application.ActiveExplorer().CurrentFolder = inBox.
            Folders[folderName];
        this.Application.ActiveExplorer().CurrentFolder.Display();
    }
    catch
    {
        MessageBox.Show("There is no folder named " + folderName +
            ".", "Find Folder Name");
    }
}

Compiling the Code

This example requires:

  • A folder named TestFolder.

See Also

Tasks

How to: Programmatically Search Within a Specific Folder

How to: Programmatically Search for a Specific Contact

How to: Programmatically Create Custom Folder Items

Concepts

Working with Folders