How to: Retrieve a Folder by Name

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

Microsoft Office version

  • Outlook 2003

  • Outlook 2007

For more information, see Features Available by Application and Project Type.

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

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
        MessageBox.Show("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: Search Within a Specific Folder

How to: Search for a Specific Contact

How to: Create Custom Folder Items

Concepts

Working with Folders