Manged outlook add-in issue

So I am having an issue with some code, that checks the mailitem.sendername and then moves it to a folder and then flags it. Just something simple, am using a switch statement to do the job... the code is as following:


void ThisApplication_NewMail()
{
Outlook.MAPIFolder _inbox =this.GetNamespace("MAPI").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items unread = _inbox.Items.Restrict("[Unread]=true");

foreach (object mailin unread)
{
Outlook.MailItem mailitem = mailas Outlook.MailItem;
if (mailitem !=null)
{
switch (mailitem.SenderName)
{
case "Debora Terrill":
mailitem.FlagIcon = Microsoft.Office.Interop.Outlook.OlFlagIcon.olGreenFlagIcon;
mailitem.Move(_inbox.Folders["People"].Folders["Debbie"]);
mailitem.Save();
break;
case "Robert Frost":
mailitem.FlagIcon = Microsoft.Office.Interop.Outlook.OlFlagIcon.olRedFlagIcon;
mailitem.Move(_inbox.Folders["People"].Folders["Robert"]);
mailitem.Save();
break;
default:
break;
}
}
}
}


the issue is that if both case statements are being used, it will only check the second one, even though there are unread e-mails from debora in the inbox. If i comment out the robert part it will do what it needs to do with the debora e-mails. I am so confused LOL any help would be nice
[2069 byte] By [wacko] at [2008-2-7]
# 1

I believe that the behavior that you are seeing is being caused by an unhandled exception that is being thrown when the mailitem.Save() command is executed.

I implemented the code that you provided and saw similar behavior when loading the add-in and running the code. Upon adding error handling I found that when executing the Save() that the following error was thrown.

"The function cannot be performed because the object has been deleted."

Commenting out the mailitem.Save() code appears to have resolved the problem for me.

Hope this helps!

Regards,

Ken Laws
MSFT

This posting is provided "AS IS" with no warranties, and confers no rights.

For more information regarding Visual Studio Tools for Office 2005:

Best of Blogs: Visual Studio 2005 Tools for Office
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_2003_ta/html/odc_landvsto2005_ta.asp

Visual Studio Tools for Office Forum
http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=16

Visual Studio Tools for the Microsoft Office System
http://msdn.microsoft.com/office/understanding/vsto/default.aspx

KenLaws at 2007-9-8 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...
# 2
my question is then cannot you not have two mailitem.save()'s because if i comment out one of the case blocks such as the robert case block it works just fine. If I do not save the mailitem it will not save the flag to the e-mail. On top of that as I walk through the code... I have no clue what item it cannot find because an unread e-mail from Debbie is in the inbox and it doesn't move that but it does move and flag the e-maisl from robert... Is it tryin to find the robert e-mail again ? and it can't save the e-mail because its not there ? or is it an issue with the debbie e-mail ?
wacko at 2007-9-8 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...
# 3
you know Ken, after looking at this again, you where right the save was the issue but not because it was there but where it was in the code... For some reason it will not save the e-mail or it will throw the error if the save statement is after the move statement... I am assuming that is because it assumes the e-mail is in the Inbox, to me this is kinda lame because the e-mail is just an object. I mean should it matter where it is and it knows where the item is when it moves it. So I don't know if this I by design or an actuall issue.
wacko at 2007-9-8 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...
# 4

You are correct, I forgot to mention in my earlier response but I do believe it is the location of the Save command within the code that was the problem.

Per the Outlook VBA help documentation for the Save method.

"Saves the Microsoft Outlook item to the current folder or, if this is a new item, to the Outlook default folder for the item type."

Based on this it would seem that this behavior is by design.

Regards,

Ken Laws
MSFT

This posting is provided "AS IS" with no warranties, and confers no rights.

For more information regarding Visual Studio Tools for Office 2005:

Best of Blogs: Visual Studio 2005 Tools for Office
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_2003_ta/html/odc_landvsto2005_ta.asp

Visual Studio Tools for Office Forum
http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=16

Visual Studio Tools for the Microsoft Office System
http://msdn.microsoft.com/office/understanding/vsto/default.aspx

KenLaws at 2007-9-8 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...
# 5
my question is then why did it work with the robert based e-mails and not the debbie based e-mails... I mean if its the current folder of the e-mail.. does that mean the current location in which the mailitem was orginally created ? or where it is at the time it was saved ? I mean here again it did work with the robert based e-mails but it would not work with any e-mail that was from debbie.
wacko at 2007-9-8 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...
# 6

When I run the code with the Save command enabled and in the original location, the behavior that I am seeing is a bit different.

For instance, if I have two e-mails from Scott and then three e-mails from Ken, the behavior that I see is that only one of the e-mails gets moved each time that the NewMail event is triggered and that the e-mail that is moved is the earliest unread e-mail. I don't see the behavior where one execution of the event removes all of the unread e-mails from Scott or Ken, the code executes until it attempts to execute the Save command and then fails out of the loop.

So why you are seeing this behavior where all of the e-mails from Robert are being moved with the Save command implemented I don't know at this point.

However, I do know that the Save command in the original implementation is triggering an exception and that if you were to move the Save method prior to executing the Move command or if you add error handling to the process you can prevent this behavior from occurring.

Regards,

Ken Laws
MSFT

This posting is provided "AS IS" with no warranties, and confers no rights.

For more information regarding Visual Studio Tools for Office 2005:

Best of Blogs: Visual Studio 2005 Tools for Office
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_2003_ta/html/odc_landvsto2005_ta.asp

Visual Studio Tools for Office Forum
http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=16

Visual Studio Tools for the Microsoft Office System
http://msdn.microsoft.com/office/understanding/vsto/default.aspx

KenLaws at 2007-9-8 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...
# 7
I am cluesless because I just tested it again and it does move all the robert e-mails and it gives me errors for all of them but it moves them and flags them.... I wonder if the move command actually saves the e-mails ? and maybe thats why there is an issue. But it does move all the robert e-mails and none of the debbie e-mails if both are not commented out.
wacko at 2007-9-8 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...
# 8
after messing around with it some more it looks like the move command saves the e-mail... because if i just remove the save command as you suggested before it works as it should. My question is why would the move command save the e-mail ? But, I guess it makes sense on some level. Is there any documentation on the managed outlook add-ins ? It would have been nice to know that move inately saves the e-mail before it moves it to its destination. I still have no clue why it did move all th robert stuff and not the debbie stuff and why your outcome was way different then mine.
wacko at 2007-9-8 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...
# 9

There's no documentation that I've been able to find to indicate that executing the Move command will also Save the mail item, although I do see the same behavior that you've mentioned where the flag icon is set for the mail item after it has been moved to the other folder when the Save command is omitted.

As a best-practice, I would recommend executing the Save command prior to executing the Move as this would insure that any changes made to the mail item were saved before the item gets moved and by executing the Save prior to moving the mail item, this should also prevent the error from being thrown.

Regarding your question concerning documentation for managed Outlook add-ins, I would refer you to the following MSDN site:

http://msdn.microsoft.com/office/understanding/vsto/default.aspx

If you have any questions please let me know via the posting.

Regards,

Ken Laws
MSFT

This posting is provided "AS IS" with no warranties, and confers no rights.

For more information regarding Visual Studio Tools for Office 2005:

Best of Blogs: Visual Studio 2005 Tools for Office
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_2003_ta/html/odc_landvsto2005_ta.asp

Visual Studio Tools for Office Forum
http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=16

Visual Studio Tools for the Microsoft Office System
http://msdn.microsoft.com/office/understanding/vsto/default.aspx

KenLaws at 2007-9-8 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...
# 10
Will do, thanks a lot for your help ken.
wacko at 2007-9-8 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...
# 11
So here is another issue that has come up because of this code... When a new e-mail arrives and it happens to be from one of those people that I am filtering for. I see the desktop notification and when i click on it to open up the e-mail I get an error... it seems it can't find the e-mail because I moved it out of the inbox folder. Does this mean that any e-mail that i filter for could not be opened from the desktop notification ?
wacko at 2007-9-8 > top of Msdn Tech,Visual Studio Tools for Office,Visual Studio Tools for Office...