Visual Studio Tools for the Microsoft Office System
How to: Retrieve a Folder by Name

Note Required applications

The code example in this topic can be compiled only if you have the required applications installed.

For more information, see Features Available by Product Combination.

  • One of these development environments:

    VSTO 2005

    -or-

    Visual Studio Team System

  • Microsoft Office Outlook 2003

NoteNote

This code does not compile if you use the VSTO 2005 SE version of the Outlook 2003 add-in project template. For more information, see Getting Started Programming Application-Level Add-ins.

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

Example

Visual Basic
Private Sub SetCurrentFolder()
    Dim folderName As String = "TestFolder"
    Dim inBox As Outlook.MAPIFolder = Me.ActiveExplorer().Session. _
        GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
    Try
        Me.ActiveExplorer().CurrentFolder = inBox.Folders(folderName)
        Me.ActiveExplorer().CurrentFolder.Display()
    Catch
        MessageBox.Show("There is no folder named " & folderName & _
            ".", "Find Folder Name")
    End Try
End Sub
C#
private void SetCurrentFolder()
{
    string folderName = "TestFolder";
    Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)
        this.ActiveExplorer().Session.GetDefaultFolder
        (Outlook.OlDefaultFolders.olFolderInbox);
    try
    {
        this.ActiveExplorer().CurrentFolder = inBox.
            Folders[folderName];
        this.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

Tags :


Page view tracker