I have an Outlook Add-in that moves an email item to another folder in another mailbox. At least, it used to do that. Now it no longer moves it. I wrote another test Add-in that moves an email item to another folder in the current user's mailbox and it does work, so I my question is, is there something about moving items to another mailbox that is causing a problem?
Here is some code to explain things:
To perform the move to another mailbox, the code creates stores for each mailbox using this line,
stores = Globals.ThisAddIn.OutlookApplication.GetNamespace("MAPI").Stores;
then iterates through each store using a foreach loop,
foreach (Outlook.Store store in stores)
It gets the current inbox, for use later,
destinationMailboxFolderInbox = (Outlook.Folder)store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
If this store DisplayName matches the one I specified as the destination, it proceeds to search the subfolders for the one I want,
if (store.DisplayName.Equals(destinationMailbox))
{
foreach (Outlook.Folder myFolder in destinationMailboxFolderInbox.Folders)
{
if (myFolder.Name.Equals(destinationMailboxFolder))
{
item.Move(myFolder);
The problem is, it does not appear to run that last foreach loop (looping through the folders).
Like I said, it works when I change it to use my current user's mailbox. It does not work when I set it to the shared mailbox that the current user can access.
Is there some other step I am missing?
It's often not a good idea to modify the contents of a (sub)set of items while looping over them. You could modify your code so that it first identifies all of the items that need to be processed, and adds them to a Collection. Then process all the items in that collection. Basically you shouldn't be removing items from the Inbox while you're looping through its contents. First collect all the items you want to process (in your Inbox loop), then when you're done looping, process that collection of items.
– PL Staggs