tags:

views:

975

answers:

3

I'm trying to get a specific folder in outlook with c#. Someone else had the same issue here http://stackoverflow.com/questions/76964/using-outlook-api-to-get-to-a-specific-folder but when using the Folders collection I'm not sure how to get through the folders collection. I mean, I've looked at the type of object that the Folders collection returns and it looks like it's a Folders object. But when I try to use that in a loop it gives me an invalid cast exception. I also hoped I could use the GetFolderFromID method to give it the string name of the folder but that doesn't want to work either... but I also can't find an example of how to use it so I'm not sure I'm coding it correctly. Here's an example of what I tried. Anyone know how to get the Processed folder which is on the same level as the Inbox folder? Thanks.

        MAPIFolder oProcessed = null;
        foreach (var folder in oNS.Folders)
            if (folder.ToString() == "Processed")
            {
                oProcessed = (MAPIFolder)folder;
            }

        if (oProcessed == null)
            throw new Exception("Missing processed folder.");
A: 

This is an inept translation from VBA, but may offer some ideas, seeing you have no answers as yet. In VBA, it is best to get the parent folder of Inbox and to look in that for folders at the same level.

        Microsoft.Office.Interop.Outlook._Folders oFolders;
        Microsoft.Office.Interop.Outlook.MAPIFolder oPublicFolder =
            olNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox).Parent;
        //Folders at Inbox level
        oFolders = oPublicFolder.Folders;
        foreach (Microsoft.Office.Interop.Outlook.MAPIFolder Folder in oFolders)
        {
            string foldername = Folder.Name;
            if (foldername == "Test")
            Console.Write(Folder.Name);
        }
Remou
+1  A: 

Hi, you need to get hold of the root level mailbox folder

 Outlook.MAPIFolder rootFolder=  Application.Session.DefaultStore.GetRootFolder();

Then loop through the rootFolder folders collection check in the names

Outlook.MAPIFolder processedFolder = null;
          foreach (Outlook.MAPIFolder folder in rootFolder.Folders)
          {
              if (folder.Name == "Processed")
              {
                  processedFolder = folder;
                  break;
              }
          }

Check out http://msdn.microsoft.com/en-us/library/bb176810.aspx to get you head round the API.

Marcus

76mel
A: 

If you have the folder path as a string, you can use this function:

private MAPIFolder GetFolder(string folderPath, Folders folders)
{
    string dir = folderPath.Substring(0, folderPath.Substring(4).IndexOf("\\") + 4);
    try
    {
        foreach (MAPIFolder mf in folders)
        {
            if (!(mf.FullFolderPath.StartsWith(dir))) continue;
            if (mf.FullFolderPath == folderPath) return mf;
            else
            {
                MAPIFolder temp = GetFolder(folderPath, mf.Folders);
                if (temp != null)
                    return temp;
            }
        }
        return null;
    }
    catch { return null; }
}
awaitzbe