none
I want to ask about ItemAdd event in outook? RRS feed

  • Question

  • Hi buddies,

    1.I drag & drop a mail to the outlook folder, i can cast the object item to MailItem.

    When i drag & drop multiple mails to the outlook folder, how can i cast the object item to get mail items?

    2.Also i have the same question for attachment file, i can cast the object item to DocumentItem.

    But when i drag & drop multiple attachment files,  the item add event is triggered only once.

    And when i can not cast the object item to attachments, how can i cast the  object item to get document items?

    The itemadd event is below:

    public delegate void ItemsEvents_ItemAddEventHandler(object Item)

    Thanks.



    • Edited by 董建华 Thursday, March 13, 2014 2:06 AM
    Thursday, March 13, 2014 2:05 AM

Answers

  • Hi team, 

    I have found the root cause! Because the message box i called.

    The message box has prevent other files or mail items call the item add method.

    Though i don't know why, after i remove the message box.

    The other files or mail items i select can be added through the item add event.

    Thanks Eugene, Dmity

    • Marked as answer by 董建华 Thursday, March 13, 2014 3:54 AM
    Thursday, March 13, 2014 3:54 AM

All replies

  • The ItemAdd event should be fired for each item you drop to a folder. But there is a known issue in the Outlook object model - if you drop more than 16 items the event may not be fired at all.
    Thursday, March 13, 2014 2:15 AM
  • Please show the relevant snippets of your code.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, March 13, 2014 2:16 AM
  • Hi Eugene,

    When i drop file or mail item  to the folder  one by one,  the ItemAdd event has been fired for each item.

    When select multiple 2  files or mail items at the same time, the ItemAdd event has been fired only once.Can you tell me how to fire the event for each file or mail item in this scenario?

    Thursday, March 13, 2014 2:46 AM
  •      

    Hi Dmity,

    Here is the code, i use this method for ItemAdd event.

    private void ItemAdd(object obj)
            {
                Outlook.Selection  ssss= Application.ActiveExplorer().Selection;
                DocumentItem docitem = obj as DocumentItem;
                MailItem mailitem = obj as MailItem;
                //Inspector ins = obj as Inspector;
                //Outlook.Selection sec = obj as Outlook.Selection;
                //Document doc = obj as Document;
                //Attachment attac = obj as Attachment;
                //AttachmentSelection atc = obj as AttachmentSelection;
                MessageBox.Show("hello word");
            }



    • Edited by 董建华 Thursday, March 13, 2014 2:53 AM
    Thursday, March 13, 2014 2:49 AM
  • Why are you using Explorer.Selection instead of using the object (new item) passed to your ItemAdd event handler?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, March 13, 2014 2:57 AM
  • Do you keep the Items object that fires the events in a  global/class variable to make sure it does not get garbage collected?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, March 13, 2014 2:58 AM
  • Hi Dmity,

    I know i should use the object(new item). As the event is fired once when multiple files drop  to folder at the same time, i can only get the first file from the object, so try to use selection to get other files.



    • Edited by 董建华 Thursday, March 13, 2014 3:12 AM
    Thursday, March 13, 2014 3:06 AM
  • Hi Dmity,

    Yes, i use a global dictionary like below to store events :  

     Dictionary<MAPIFolder, Items> ItemAddEvents = new Dictionary<MAPIFolder, Items>();

    The item event worked well for each folder.But for one folder, when drop multiple files to it, the ItemAdd event is fired only once.I can only get the first file from the object parameter, i don't know how to get other files




    • Edited by 董建华 Thursday, March 13, 2014 3:15 AM
    Thursday, March 13, 2014 3:10 AM
  • Anyone has ideas?Tell me please.Is this a known issue?
    • Edited by 董建华 Thursday, March 13, 2014 3:42 AM
    Thursday, March 13, 2014 3:38 AM
  • Hi team, 

    I have found the root cause! Because the message box i called.

    The message box has prevent other files or mail items call the item add method.

    Though i don't know why, after i remove the message box.

    The other files or mail items i select can be added through the item add event.

    Thanks Eugene, Dmity

    • Marked as answer by 董建华 Thursday, March 13, 2014 3:54 AM
    Thursday, March 13, 2014 3:54 AM