0

I am working on an Outlook AddIn with a custom ribbon. The user opens a mail item in Read mode and clicks a button on the ribbon and the program will move the email to a folder (not the user's personal mailbox, but rather to a different mailbox that user has access to).

When the program is run, it works the first time, but the second time the user runs it, it throws an error:

"The attempted operation failed. An object could not be found."

Here is the relevant code:

(in ThisAddIn.cs)

public partial class ThisAddIn
    {
        public Outlook.Application OutlookApplication;

        void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            OutlookApplication = this.Application;
        }
(etc)

(In Ribbon1.cs, in a method that gets called upon button_Click)

Outlook.Inspector inspector = Globals.ThisAddIn.OutlookApplication.ActiveInspector();
Outlook.MailItem item = inspector.CurrentItem as Outlook.MailItem;
Outlook.Stores stores = null;
Outlook.Folder destinationMailboxFolderInbox = null;

and

try
{
    // Set the mailbox move location
    stores = Globals.ThisAddIn.OutlookApplication.GetNamespace("MAPI").Stores;

    foreach (Outlook.Store store in stores)
    {
        attachmentsFoundTotal++;
        if (store.DisplayName == destinationMailbox)
        {
            destinationMailboxFolderInbox = (Outlook.Folder)store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            try
            {
                 // the code breaks on this line below:
                item.Move(destinationMailboxFolderInbox.Folders[destinationMailboxFolder]);
            }
            catch (Exception ex3)
            {
                System.Windows.Forms.MessageBox.Show(ex3.Message + "  Could not find Outlook folder " + destinationMailboxFolder + ". The mail item was not moved."  );
            }
        }
    }
}
catch (Exception ex2)
{
    System.Windows.Forms.MessageBox.Show(ex2.Message);
}

UPDATE: After trial & error testing, the only way I could resolve the Outlook 2010 bug was to have the Outlook view switch to the folder where the mail item was moved, using this command after the command to move the item to myfolder.

Globals.ThisAddIn.OutlookApplication.ActiveExplorer().CurrentFolder = myFolder;
PL Staggs
  • 11
  • 6
  • Would that not mean that the folder destinationMailboxFolder does not exist? – Dmitry Streblechenko Sep 28 '17 at 22:34
  • Yes, that is the meaning of the error message, but the folder should exist. It existed the first time I clicked the button. Perhaps when the mail item is moved, it changes the current context, and the answer is to somehow reset it back to the inbox where the next item is. I noticed that when I click the button the first time, then I select another folder then I go back to the first folder and click the button a second time on the next item, it works without error. – PL Staggs Sep 29 '17 at 16:03
  • Are you sure you have the right store? – Dmitry Streblechenko Sep 29 '17 at 17:24
  • Yes, I have the right store. I check for it using line "if (store.DisplayName == destinationMailbox)" – PL Staggs Sep 29 '17 at 20:41
  • I wonder if the problem is related to how I have methods that open a new Outlook.Application() each time. Maybe I'm only supposed to do that once for the entire AddIn. If so, how would I do that? Where would I place that call, and how would I reference that from the methods? I have a ThisAddIn.cs file that is mostly an empty shell. I have other "Ribbon1.cs" type files that contain a "Ribbon1_Load" and a "button_Click" methods. – PL Staggs Sep 29 '17 at 20:45
  • That would not matter, but you should never create a new instance of Outlook.Application - your addin gets one for free (use the Globals object) – Dmitry Streblechenko Sep 29 '17 at 20:57
  • As a test, loop through all subfolders of destinationMailboxFolderInbox and log their names – Dmitry Streblechenko Sep 29 '17 at 21:02
  • OK, I used Globals, following this post ( https://stackoverflow.com/questions/2175803/how-to-access-application-property-in-vsto-outlook-add-in-outside-of-thisaddin-c ). I edited the code above to the new version (Globals.ThisAddIn.OutlookApplication ...). I will work on looping through the subfolders and see what I find. – PL Staggs Sep 29 '17 at 21:25
  • After much testing, I believe it is a bug. I opened Outlook, moved a mail item manually, then clicked the button on another item and it did not move. I re-opened Outlook, clicked the button on an item and it moved and then I clicked the button on another item and it did not move. So either through my code or Outlook UI's code, the second move does not work. Something must be wrong with the way Outlook 2010 moves the items. Fortunately I can work around this. I have another way. Thanks for your help. Your "loop" suggestion is the answer that helped me figure it out. – PL Staggs Sep 29 '17 at 22:35

1 Answers1

0

When the program is run, it works the first time, but the second time the user runs it, it throws an error:

The Outlook UI is not refreshed when an item is moved to another place. You need to refresh the view on your own to get a live reference. Any UI objects still hold an old reference.

For example, the Move method moves a Microsoft Outlook item to a new folder and returns an Object value that represents the item which has been moved to the designated folder.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45