0
votes

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?

1
Update: I found another post that mentions something that may be related: 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
Does it fail for all items? Or just some?Dmitry Streblechenko
When you say "does not appear to run that last foreach loop", that means the previous statement (store.DisplayName.Equals(destinationMailbox)) returns false, which means store's display name is different from what you expect.Dmitry Streblechenko
If fails for all items. During testing, I added some output statements in between the first foreach loop and the second foreach loop. It displays the output statements only for the first one.PL Staggs
I will double-check tomorrow and confirm what I saw during testing.PL Staggs

1 Answers

0
votes

This problem was either a permissions issue or a bug. Instead of resolving directly, I ended up developing a workaround.

The workaround was to create an Outlook "Quick Step" button to move the email. The Outlook user will click my VSTO-coded Add-In button to save the attachments and then click the Quick Step button to move the email to the folder.