|
Ryan Taylor
Posts: 46
Joined: 2008-04-08
|
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[i];
if (folder.Name == _folderSettings.OutlookFolderViewerName)
{
customFolder = folder;
break;
}
}
if (customFolder == null)
{
try
{
customFolder = folders.Add(_folderSettings.OutlookFolderViewerName, 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 into what the difference between cached exchange mode and non-cached exchange mode, I would greatly appreciate hearing your thoughts!
Thanks,
Ryan |
|
Posted 03 Oct, 2008 16:13:26
|
|
Top
|
|
Eugene Astafiev
Guest
|
Hello Ryan,
What does the _folderSettings.OutlookFolderViewerName mean? |
|
Posted 06 Oct, 2008 05:34:22
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18821
Joined: 2006-05-11
|
Hello Ryan,
Try printing folder names when looping through the folders.
Andrei Smolin
Add-in Express Team Leader |
|
Posted 06 Oct, 2008 08:22:50
|
|
Top
|
|
Ryan Taylor
Posts: 46
Joined: 2008-04-08
|
Hi Gentlemen,
_folderSettings.OutlookFolderViewerName is just a string that represents the name of the folder, for example, "Test". I forgot to replace it when copying the code to the forum.
Andrei,
When I printed the folder names in my loop "Test" never appeared (when use cached mode is checked), even though it exists in Outlook. Since it is not found, the code attempts to create a folder with that name and fails.
If cache mode is unchecked, then everything works as expected.
Thanks,
Ryan |
|
Posted 06 Oct, 2008 09:53:09
|
|
Top
|
|
Eugene Astafiev
Guest
|
|
Posted 06 Oct, 2008 10:46:47
|
|
Top
|
|
Ryan Taylor
Posts: 46
Joined: 2008-04-08
|
Hi Eugene,
I have read the link you provided but I didn't see anything that pointed to the problem. Did I miss something?
Thanks,
Ryan |
|
Posted 06 Oct, 2008 17:48:09
|
|
Top
|
|
Eugene Astafiev
Guest
|
Hi Rayn,
If anyone has any insight into what the difference between cached exchange mode and non-cached exchange mode, I would greatly appreciate hearing your thoughts!
The link I've published describes the Cached Exchange Mode.
I hope the following http://technet.microsoft.com/en-us/library/aa996244(EXCHG.65).aspx will help you to fix the issue. |
|
Posted 07 Oct, 2008 04:59:03
|
|
Top
|
|
Ryan Taylor
Posts: 46
Joined: 2008-04-08
|
Hi Eugene,
Thanks for the additional link. It turns out that my loop was wrong. (1 based lists....)
for (int i = 1; i < folders.Count; i++)
{...}
should be:
for (int i = 1; i <= folders.Count; i++)
{...}
Thanks for your help!
Ryan |
|
Posted 07 Oct, 2008 11:55:39
|
|
Top
|
|
Eugene Astafiev
Guest
|
Not at all! You are welcome, Ryan. |
|
Posted 07 Oct, 2008 15:26:23
|
|
Top
|
|