none
BeforeAttachmentAdd not triggered if user creates e-mail with "File Explorer - Context Menu - Send to - Mail recipient" RRS feed

  • Question

  • Hello everybody :)

    I developed an AddIn that checks which attachments are added to an e-mail by waiting for the event BeforeAttachmentAdd. It works fine, except in the case the user selects a file in the file system and creates the e-mail via "Context Menu - Send to - Mail recipient". Then the event is not triggered.

    Additionally when this e-mail is open and the user decides to add more files and presses the "Attach File" button - again the event is not triggered.

    It seems like the AddIn is not started.

    1) Any explanation why this is happening?

    2) Any suggestions on how to fix this?

    BTW, I using VS2015 and was checking against Outlook 2013 and 2016.

    Looking forward to hear from you soon :)

    BR,

    Harald



    • Edited by Hago67 Tuesday, September 13, 2016 3:04 PM
    Tuesday, September 13, 2016 2:34 PM

Answers

  • Keep in mind that some events (such as Inspectors.NewInspector) do not fire if the message is shown using Simple MAPI or through a mailto link.

    What you can do is periodically (timer?) scan the Inspectors collection to check if there is a new inspector you have not yet handled. If yes, run the code that you normally run in the NewInspector event.


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

    • Marked as answer by Hago67 Wednesday, September 14, 2016 2:57 PM
    Tuesday, September 13, 2016 5:44 PM

All replies

  • Hello Harald,

    > I developed an AddIn that checks which attachments are added to an e-mail by waiting for the event BeforeAttachmentAdd.

    Did you have a chance to check whether the add-in is listed in the COM add-ins dialog? Is it listed as enabled or disabled?

    Anyway, the code where you subscribe to the BeforeAttachmentAdd event is not reached/called. You need to handle the NewInspector/Activate events and then subscribe to the BeforeAttachmentAdd event if you are still not subscribed to the event. The fact is that the Startup event can be skipped in that case (not fired at all).

    What code exactly do you have now? Where and when do you subscribe to the events?


    [custom.development]

    Tuesday, September 13, 2016 5:05 PM
  • Keep in mind that some events (such as Inspectors.NewInspector) do not fire if the message is shown using Simple MAPI or through a mailto link.

    What you can do is periodically (timer?) scan the Inspectors collection to check if there is a new inspector you have not yet handled. If yes, run the code that you normally run in the NewInspector event.


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

    • Marked as answer by Hago67 Wednesday, September 14, 2016 2:57 PM
    Tuesday, September 13, 2016 5:44 PM
  • Hi Eugene,

    my code is based on https://msdn.microsoft.com/en-us/library/cc668191.aspx

    I subscribe to the event in Inspectors_NewInspector().

    After a check - the AddIn is listed as enabled.

    Cheers,

    Harald

    • Edited by Hago67 Tuesday, September 13, 2016 6:19 PM
    Tuesday, September 13, 2016 6:06 PM
  • Hi Dmitry,

    thanks for that info - that's new for me. :)

    I guess a timer solution won't catch the initial attachment triggered by the "Send To" action?

    It also implies that Outlook is already started, which might not always be the case.

    BTW, would you be so kind to have a look at a different reply/topic: https://social.msdn.microsoft.com/Forums/vstudio/en-US/cb3d4913-9045-41b0-902a-7fb7e6938af3/ribbon-attachment-bug-in-outlook-2016?forum=vsto#43cc2897-4f86-45f1-adbf-f1a8b4f04d7c ?

    Many thanks,

    Harald


    • Edited by Hago67 Tuesday, September 13, 2016 6:17 PM
    Tuesday, September 13, 2016 6:17 PM
  • It would still work fine even if there are already existing attachments - for each (unprocessed) inspector, loop through all existing attachments as if they were just added.

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

    Tuesday, September 13, 2016 7:45 PM
  • It seems the NewInspector is not fired in that case. You need to check for existing Inspector windows and if any subscribe to the BeforeAttachmentAdd event.

    [custom.development]

    Wednesday, September 14, 2016 3:44 PM
  • I agree - the question is "when". I mean in which method?

    Do you think ThisAddIn_Startup() is right? Because this is the earliest method that gets called.

    Wednesday, September 14, 2016 4:03 PM
  • You need to *periodically* scan the Inspectors collection. Doing it just once (ThisAddIn_Startup etc.) is not enough.

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

    Wednesday, September 14, 2016 4:06 PM
  • Understood - because you never know how often the user clicked "Send to" outside of Outlook.

    Is that the reason why in https://msdn.microsoft.com/en-us/library/office/ee692172.aspx all Inspector and Explorer classes get wrapped?

    Wednesday, September 14, 2016 4:14 PM
  • Because this way you can store references to various objects (such as the Inspector itself)  that might no be available as parameters when various events fire. 

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

    Wednesday, September 14, 2016 4:43 PM
  • Yep. I bielive you need to check the Inspectors collection in the Startup event handler.

    [custom.development]

    Wednesday, September 14, 2016 5:44 PM
  • Dmitry,

    Could you please point me to any issue or reason why we should check the Inspectors collection periodically instead of subscribing to the NewInspector event?


    [custom.development]

    Wednesday, September 14, 2016 5:45 PM
  • Hago,

    You will know if you subscribe to the NewInspector event. You just need to check the Inspectors collection at startup for existing inspectors. Later you will be notified.


    [custom.development]

    Wednesday, September 14, 2016 5:46 PM
  • Because NewInspector event does not always fire - this is done on purpose.

    Easy to check in OutlookSpy - click Application button, select Inspectors property, click Browse. Go to the Events tab. Open an existing message or hit Ctrl+N to create a new one - NewInspector event is logged.

    Open Windows Explorer, right click on any file, select "Send To | Mail Recipient". New inspector is open. NewInspector event does not fire.


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

    Wednesday, September 14, 2016 6:13 PM
  • Dmitry,

    There is no need to use OutlookSpy.

    > NewInspector does not always fire

    > Open Windows Explorer, right click on any file, select "Send To | Mail Recipient". New inspector is open.

    Do you know any other conditions under which the NewInspector event is not fired? Why should we check the Inspectors collection periodically?


    [custom.development]

    Wednesday, September 14, 2016 6:50 PM
  • Of course there is no need to use OutlookSpy. There is no need to use Outlook either - you can use a free telnet client to connect to an SMTP server and send any message you want. Just a matter of convenience...

    Messages displayed using Simple MAPI are the only case I am aware  of when Outlook disables its events.


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

    Wednesday, September 14, 2016 7:21 PM
  • An add-in or VBA macro can be created if I need to check something. OutlookSpy is useless in that case.

    I don't think there is a need for scanning the Inspectors collection *periodically* even for that special case.


    [custom.development]

    Wednesday, September 14, 2016 7:35 PM
  • If it takes you less time to write some VBA code or create an addin in VS instead of clicking 3 times in Outlook, my hat goes off to you. I am probably too dumb compared to you...

    There were two questions asked 1. Why that happens? 2. Are there any workarounds? As for whether there is a need to scan the Inspectors collection, it is up to the original poster.


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

    Wednesday, September 14, 2016 7:46 PM
  • Hello again,

    I was off topic and occupied with company internal meetings.

    Today I had time to look into this topic again and found that if Outlook gets started via "Send To - Mail recipient" the application object in ThisAddIn_Startup() returns for the calls ActiveExplorer(), ActiveInspector() and ActiveWindow() always null.

    A check at a later time seems to be the right answer to this situation.

    Of course I am open for other suggestions :)

    Tuesday, September 27, 2016 4:46 PM