none
BeforeAttachmentRead not working RRS feed

  • Question

  • Hey. I've recently been coding an add-in for Outlook 2016. I want to hook up my code to the Attach files--> Browse Locally Button. Been trying for quite a long time, I haven't figured out any way to make MailItem.BeforeAttachmentRead(my_event_handler) work in my add-in. I guess the Microsoft guys made it uselessly, isn't it?

    Please Help.

    My Code:

    mailItem.BeforeAttachmentAdd = new Outlook.ItemEvents_10_BeforeAttachmentReadEventHandler(check);

    ...

    private void check(Outlook.Attachment Attachment, ref bool Cancel)

    {

    MessageBox.Show("Called");

    }

    The check() method is never called.

    Tuesday, June 6, 2017 12:28 PM

Answers

  • Hello,

    We could registry Application.ItemLoad Event (Outlook) firstly and then registry MailItem.BeforeAttachmentRead Event (Outlook).

    The ItemLoad event should typically be implemented as a means to hook up item-level event handlers such as BeforeRead, Open, Send, and Write.

    E.g.

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.ItemLoad += Application_ItemLoad;
            }
            private void Application_ItemLoad(object Item)
            {
                if (Item is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = Item as Outlook.MailItem;
                    //mailItem.PropertyChange += MailItem_PropertyChange;
                    //mailItem.AttachmentAdd += MailItem_AttachmentAdd;
                    mailItem.BeforeAttachmentRead += MailItem_BeforeAttachmentRead;
                    //mailItem.BeforeAttachmentAdd += MailItem_BeforeAttachmentAdd;
                }
            }
            private void MailItem_BeforeAttachmentRead(Outlook.Attachment Attachment, ref bool Cancel)
            {
                MessageBox.Show("MailItem_BeforeAttachmentRead");
            }

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 7, 2017 3:24 AM
    Moderator

All replies

  • It's

    mailItem.BeforeAttachmentRead += new Outlook.ItemEvents_10_BeforeAttachmentReadEventHandler(check);

    Tuesday, June 6, 2017 12:29 PM
  • Hello,

    We could registry Application.ItemLoad Event (Outlook) firstly and then registry MailItem.BeforeAttachmentRead Event (Outlook).

    The ItemLoad event should typically be implemented as a means to hook up item-level event handlers such as BeforeRead, Open, Send, and Write.

    E.g.

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.ItemLoad += Application_ItemLoad;
            }
            private void Application_ItemLoad(object Item)
            {
                if (Item is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = Item as Outlook.MailItem;
                    //mailItem.PropertyChange += MailItem_PropertyChange;
                    //mailItem.AttachmentAdd += MailItem_AttachmentAdd;
                    mailItem.BeforeAttachmentRead += MailItem_BeforeAttachmentRead;
                    //mailItem.BeforeAttachmentAdd += MailItem_BeforeAttachmentAdd;
                }
            }
            private void MailItem_BeforeAttachmentRead(Outlook.Attachment Attachment, ref bool Cancel)
            {
                MessageBox.Show("MailItem_BeforeAttachmentRead");
            }

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 7, 2017 3:24 AM
    Moderator