Add Outlook (2007) folder above Inbox when using Cached Exchange M

  • Thread starter Thread starter Ryan Taylor
  • Start date Start date
R

Ryan Taylor

Hello,

I am using Outlook 2007 and am attempting to create and later reference a
folder above the Inbox folder. It turns out, that this code works perfectly
when the "Use Cached Exchange Mode" option is unchecked. When it is checked,
the code acts differently in two ways: 1, when looping through the folders,
my custom folder is not found (even though it exists), 2, since the folder is
not found, it attempts to create it, this create attempt fails with an
exception.

The exception is "System.Runtime.InteropServices.COMException (0x80020009):
Cannot create the folder.
at Microsoft.Office.Interop.Outlook.FoldersClass.Add(String Name, Object
Type)"

The code I use is below, basically all the code does is loop through the
list of folders directly above the inbox folder and if it finds our folder it
returns it, if our folder isn't found, then it is created and returned:

====================================================
private MAPIFolder GetCustomFolder()
{
MAPIFolder customFolder = null;
NameSpace ns = _app.Session;
MAPIFolder mapiFolder = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
MAPIFolder parentFolder = mapiFolder.Parent as MAPIFolder;
if (parentFolder != null)
{
Folders folders = parentFolder.Folders;
// find our folder
if (folders != null && folders.Count > 0)
{
for (int i = 1; i < folders.Count; i++)
{
MAPIFolder folder = folders;
if (folder.Name == "test")
{
customFolder = folder;
break;
}
}
if (customFolder == null)
{
try
{
customFolder = folders.Add("test", OlDefaultFolders.olFolderInbox);
}
catch (Exception ex)
{
_log.Error(ex);
}
}
Marshal.ReleaseComObject(folders);
}
Marshal.ReleaseComObject(parentFolder);
Marshal.ReleaseComObject(mapiFolder);
Marshal.ReleaseComObject(ns);

}
return customFolder;
}
====================================================

If anyone has any insight as to why I am seeing a difference between cached
exchange mode and non-cached exchange mode, I would greatly appreciate
hearing your thoughts!

Thanks,
Ryan
 
Please disregard this question. It turns out my loop was wrong, I needed a
<=, instead of simply <. When comparing, it looks like my custom folder is
returned in a different in order depending on cached or non cached mode.

For non cached mode: it was returned 7th (out of 14)
For cached mode: it was returned 14th (out of 14th)

Thanks,
Ryan
 
Back
Top