A MAPIFolder's Folders collection fail in Outlook 2003

Add-in Express™ Support Service
That's what is more important than anything else

A MAPIFolder's Folders collection fail in Outlook 2003
But works perfectly in Outlook 2007 
Bargholz Thomas




Posts: 242
Joined: 2006-12-18
Hi,
I have an add-in for Outlook, which create and maintain some folders in the users mailbox.
My code was developed on Outlook 2007 and works perfectly. Now it's being debugged on Outlook 2003 and fail miserably.
As part of my code, I try to locate if the folder already exists, before adding it, in code like this:

public void AddNewFolders()
{
    if (this.RootFolder == null)
        return;

    if (this.dataService == null)
        return;

    List<Folder> myFolders = dataService.MyFolders;
    MAPIFolder item = null;
    long id;
    bool found = false;

    foreach (Folder cas in myFolders)
    {
        if (cas.RootFolder == null)
            continue;
        found = false;
        for (int i = 1; i < [B]this.RootFolder.Folders.Count[/B]+ 1; i++)
        {
            item = this.TeamShareFolder.Folders[i];
            if (item.Description.StartsWith(PROP_FOLDER_ID))
            {
                id = Convert.ToInt64(item.Description);
                if (id == cas.ID)
                {
                    found = true;
                    break;
                }
            }
        }
        if (!found)
            this.AddSingleWorkArea(cas);
    }
}


It fails on the this.ParentFolder.Folders.Count part, but only in Outlook 2003. The error message is: "The operation failed." and error code -521125883. The TargetSite is {Microsoft.Office.Interop.Outlook.Folders get_Folders()}

Pretty much everywhere I try to access a collection of folders fail in Outlook 2003 (but not on 2007).

I'm using the version neutral PIA's, Add-in-express 4.4.1913, VS 2008, .NET 3.0, XP (latest sp and patch level) and Office 2007 and Office 2003 (both on latest SP and patch level).

What could cause such behaviour?

Regards
Thomas

Posted 08 Jun, 2009 10:13:17 Top
Bargholz Thomas




Posts: 242
Joined: 2006-12-18
It appears that the issue is solved. It looks as if I hit the maximum number of open RPC connections to Exchange (256) because of the two loops increasing new MAPIFolder allocastions and RPC connections.
To get around the 256 max connections, I changed the code to release the MAPIFolder item inside the outer loop (added a try..finally around the inner loop and a Marhsal.ReleaseCOMObject, and threw in a GC.Collect in the finally part as well, so the RPC connections gets closed and the MAPIFolder items garbage collected more frequently and faster). And now it runs smoothly.

Why it does not happen for Outlook 2007, must be because it does a better job of cleaning connections?!

If you have an idea of things I should be aware of, please let me know.

Regards
Thomas
Posted 08 Jun, 2009 11:54:49 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Thomas,

I would not run GC.Collect at all.

Why it does not happen for Outlook 2007, must be because it does a better job of cleaning connections?!


Probably, you are connected to different Exchange server versions.

Sorry, I have no ideas currently.


Andrei Smolin
Add-in Express Team Leader
Posted 08 Jun, 2009 12:26:19 Top
Bargholz Thomas




Posts: 242
Joined: 2006-12-18
No, same Exchange server, same account, same data, same add-in - only the Outlook versions differ. That's why I would have expected the same error.

Anyway, it appears to run perfectly after adding the new code, on both Outlook versions, so I stick to this code unless I experience. I know GC.Collect shouldn't be called too often in your code, but if we don't experience any problems during testing, I'll keep the current code.

Regards
Thomas
Posted 09 Jun, 2009 04:47:54 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Sure.


Andrei Smolin
Add-in Express Team Leader
Posted 09 Jun, 2009 09:12:59 Top