Reputation:
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:
Inbox
Drafts
MyCustomFolder
How do I get MyCustomFolder
, which is not a subfolder of the inbox, as a MAPIFolder
or Folder
?
Upvotes: 1
Views: 1262
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