'VSTO: Does dragging and dropping a mail message into a MAPI folder cause the folder's Items.ItemAdd event to fire?
The handler I've attached to the folder's ItemAdd event is not firing. I don't know why. The variable folderTestItems
is declared at class-level, so it shouldn't be garbage-collected, right? I've walked through the code in the debugger, and the handler is being attached: folderTestItems.Items.ItemAdd += Items_ItemAdd;
I am dragging a message from folder Inbox into folder TEST. Does that action not cause the ItemAdd
to fire? If not, how do I detect that action?
public partial class ThisAddIn
{
Outlook.MAPIFolder folderTestItems;
<snip>
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.NameSpace nameSpace = this.Application.GetNamespace("MAPI");
Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
folderTestItems = folderInbox.Folders["TEST"];
if (folderTestItems != null)
{
// we do get here
folderTestItems.Items.ItemAdd += Items_ItemAdd;
}
}
private void Items_ItemAdd(object Item)
{
// we never get here
if (Item is Outlook.MailItem)
{
Outlook.MailItem mailItem = (Item as Outlook.MailItem);
string itemMessage = "The item is an e-mail message." +
" The HTMLBody is " + mailItem.HTMLBody + ".";
mailItem.Display(false);
MessageBox.Show(itemMessage);
}
}
}
Solution 1:[1]
The object that fires the event must be kept alive - you are setting the event handler on a temporary variable created by the compiler.
If you want to see which events fire and when, take a look at the folder with OutlookSpy (I am its author) - go to the folder in question, click Folder button on the OutlookSpy ribbon, select the Items property, click Browse, go to the Events tab, look at the log at the bottom of the tab.
public partial class ThisAddIn
Outlook.Items _items;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
...
_items = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Items;
_item.ItemAdd += Items_ItemAdd;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |