user3092503
user3092503

Reputation:

How can I get a MAPI Folder that isn't a subfolder of Inbox?

There are many questions that ask how to get a folder in Outlook, but all the answers I've seen assume that this folder is nested underneath the inbox folder. Even Microsoft's documentation assumes this:

private void SetCurrentFolder()
{
    string folderName = "TestFolder";

    //THIS STATEMENT ASSUMES WE'RE LOOKING IN THE INBOX
    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");
    }
}

Let's say that I have the following folders at the root level of my mailbox:

How do I get MyCustomFolder, which is not a subfolder of the inbox, as a MAPIFolder or Folder?

Upvotes: 1

Views: 1262

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

Assuming the folder is a peer of the Inbox folder, you can get the Inbox, then go up one level, then retrieve the folder in question:

Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)
        this.Application.ActiveExplorer().Session.GetDefaultFolder
        (Outlook.OlDefaultFolders.olFolderInbox);
Outlook.MAPIFolder rootFolder = (Outlook.MAPIFolder)inBox.Parent;
Outlook.MAPIFolder myFolder = rootFolder.Folders["MyCustomFolder"];

Upvotes: 1

Related Questions