In an add-in for Outlook 2010, how can I make a delete operation undo-able?

1.7k views Asked by At

I am writing an add-in for Outlook 2010. At one point it needs to delete the Mail items currently selected by the user. I'm using the following code, which works quite well:

Selection selectedMessages = Globals.ThisAddIn.Application.ActiveExplorer().Selection;

// It is possible for a non-mail item to be part of this collection.  (One example is when a calendar
// item is in the deleted items folder.  Select it and hit this delete button.)
System.Collections.IEnumerator enumerator = selectedMessages.GetEnumerator();
while(enumerator.MoveNext())
{
  if (enumerator.Current is MailItem)
  {
    ((MailItem)(enumerator.Current)).Delete();
  }
}

My problem is that when I delete messages this way, the normal "undo" operation is not available to the user. It is possible for the user to go to the Deleted Items folder and move the messages back to the Inbox. But it will be confusing for users that are used to just hitting Ctrl-Z or the little "Undo" arrow at the upper-left corner of the screen.

Is there some way I can register this action with the Undo mechanism, or perhaps invoke the "real" delete functionality of Outlook on the message so that Undo is available automatically?

1

There are 1 answers

2
CesarGon On

Don't delete the MailItem; move it to the olFolderDeletedItems folder instead. You can use GetDefaultFolder() to obtain a reference to this folder; see here.