How to: Programmatically create custom folder items

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

This example creates a new folder in Microsoft Office Outlook. The name of the user who is logged on is used for the folder name.

Applies to: The information in this topic applies to VSTO Add-in projects for Outlook. For more information, see Features available by Office application and project type.

Example

private void CreateCustomFolder()
{
    Outlook.Folder inBox = (Outlook.Folder)
        Application.ActiveExplorer().Session.GetDefaultFolder
        (Outlook.OlDefaultFolders.olFolderInbox);
    string userName = (string)this.Application.ActiveExplorer()
        .Session.CurrentUser.Name;
    Outlook.Folder customFolder = null;
    try
    {
        customFolder = (Outlook.Folder)inBox.Folders.Add(userName,
            Outlook.OlDefaultFolders.olFolderInbox);
        MessageBox.Show("You have created a new folder named " +
            userName + ".");
        inBox.Folders[userName].Display();
    }
    catch (Exception ex)
    {
        MessageBox.Show("The following error occurred: " + ex.Message);
    }
}

See also