none
Need an event before saving an attachment from "Opening Mail Attachment" dialog RRS feed

  • Question

  • Hi,

    In Outlook 2007/2010 when double clicked on mail attachment files types such as zip, docx, etc... a dialog is shown with the following text.

    Title - "Opening Mail Attachment"
    Details - "You should only open attachments from a trustworthy source.
    ....
    Would you like to open the file or save it to your computer?"
    Buttons - Open, Save and Cancel
    Checkbox selected but disabled, with text, "Always ask before opening this type of file".

    Before opening or saving an attachment I need to do some validation. Therefore I have bound to the following events,

    ((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)mailItem).BeforeAttachmentPreview
    ((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)mailItem).BeforeAttachmentRead

    These trigger when the attachment is,
    => Selected by clicking on it,
    => Selecting "Open" and "Save As" from the attachment context menu, shown when right clicked on the attachment.
    => And when "Open" is selected from the above "Opening Mail Attachment" dialog.

    But when "Save" is selected from the above "Opening Mail Attachment" dialog any of these events does not trigger.
    Is there any event that could be used before the "Save As File" dialog is shown once "Save" is clicked on "Opening Mail Attachment"?

    I have tried binding the following, but this triggers after the "Save As File" dialog.

    ((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)mailItem).BeforeAttachmentWriteToTempFile

    Alternatively if the below could be done, it would be great.
    => Binding to the double click on the attachment before the "Opening Mail Attachment" dialog.
    => Automatically selecting "Open" from the "Opening Mail Attachment" dialog when it is shown.
    => Suppressing the dialog.(May not be able to set keys in the Windows registry as the user may not have administrative privileges)

    I am using VS2010, C#.
    Any help, advice will be appreciated.


    • Edited by Damian Muller Thursday, March 19, 2015 9:24 AM Include programming language
    Thursday, March 19, 2015 9:22 AM

Answers

  • Hi all,

    Thanks for your comments. After some research I found a workaround, this may helps someone.

    When the addin loads the following is done to click on the Open button of the "Opening Mail Attachment" dialog so that the attachment is opened when double clicked on it.

    The following only considers one inspector window, to support multiple, each inspector may need to be stored, bound to the Deactivate and released when the Inspector is closed, binding to the Application.ActiveExplorer().Close.

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
    
    [DllImport("user32.dll", SetLastError = true)]
    public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
    
    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, String lpszClass, String lpszWindow);
    
    Application.ActiveExplorer().Deactivate += OnWindowDeactivate;
    Application.Inspectors.NewInspector += OnNewInspectorWindow;
    
    public void OnNewInspectorWindow(Inspector inspector)
    {
      inspector.Deactivate += OnWindowDeactivate;
    }
    
    public void OnWindowDeactivate()
    {
      IntPtr hwndScreen = IntPtr.Zero;
      hwndScreen = FindWindow((string)null, "Opening Mail Attachment");
      if (hwndScreen != IntPtr.Zero)
      {
        const int nChars = 256;
        System.Text.StringBuilder Buff = new System.Text.StringBuilder(nChars);
        uint BN_CLICKED = 245;
        IntPtr hwndScreen2 = IntPtr.Zero;
        hwndScreen2 = FindWindowEx(hwndScreen, IntPtr.Zero, (string)null, "&Open");
        SendMessage(hwndScreen2, BN_CLICKED, 0, 0);
      }
    }
    

    Wednesday, March 25, 2015 9:59 AM

All replies

  • How about the BeforeAttachmentSave event?
    Thursday, March 19, 2015 10:56 AM
  • BeforeAttachmentSave event does not trigger.

    Thursday, March 19, 2015 11:30 AM
  • Hello Damian,

    You are right, the BeforeAttachmentSave event is not fired in that case. You can open a support case with Microsoft. I think it should be fired, just before an attachment is saved.

    Unfortunately the Outlook object model doesn't provide any other event.

    As a workaround you may handle the AttachmentSelectionChange event of the Inspector or Explorer classes. It is fired when the user selects a different or additional attachment of an item in the active inspector programmatically or by interacting with the user interface.

    As you already know, the BeforeAttachmentWriteToTempFile event is fired right after the Save As File dialog is closed.

    Thursday, March 19, 2015 12:44 PM
  • Events BeforeAttachmentRead and BeforeAtachmentPreview can be cancellled.  Perhaps you can do your validation when these events fire so you can take advantage of the cancel parameter.
    Thursday, March 19, 2015 12:48 PM
  • Have you tried to handle these events? Are they fired? :)
    Thursday, March 19, 2015 1:04 PM
  • BeforeAttachmentSave is only supposed to fire when the attachment is saved in the message store, not to the file system:

    This event corresponds to when attachments are saved to the messaging store.
    The BeforeAttachmentSave event occurs just before an attachment is saved
    when an item is saved. If a user edits an attachment and then saves those
    changes, the BeforeAttachmentSave event will not occur at that time;
    instead it will occur when the item itself is later saved. It also does not
    occur when the attachment is saved on the hard disk using the SaveAsFile
    method.

    There's nothing in the Outlook object model to help with what you want, unfortunately.


    Ken Slovak MVP - Outlook

    Thursday, March 19, 2015 9:10 PM
    Moderator
  • BeforeAttachmentRead and BeforeAtachmentPreview events does not fire when double clicked on the attachment, nor when the Save button is clicked from the message.

    But BeforeAttachmentWriteToTempFile fires when the Save is clicked after the Save As dialog. It can be cancelled in this event. But,

    => It is confusing the user, as after the save as location is asked the process is cancelled.
    => BeforeAttachmentWriteToTempFile also fires when the attachment is selected(shown in the preview), so how can it be identified if this event is fired due to the Save button click?

    Friday, March 20, 2015 4:07 AM
  • Damian,

    > BeforeAttachmentRead and BeforeAtachmentPreview events does not fire when double clicked on the attachment, nor when the Save button is clicked from the message.

    I know. I have already tested your scenario on my machine with Outlook 2013 installed. The question was to rlwa32 because he suggested these events.

    > so how can it be identified if this event is fired due to the Save button click?

    It can't be identified... As I already stated, the Outlook object model doesn't provide any other event, see my previous reply above.

    Friday, March 20, 2015 10:15 AM
  • Yes, the name of the event is ambiguous. MSDN states the following for the BeforeAttachmentSave event:

    This event corresponds to when attachments are saved to the messaging store. The BeforeAttachmentSave event occurs just before an attachment is saved when an item is saved. If a user edits an attachment and then saves those changes, the BeforeAttachmentSave event will not occur at that time; instead it will occur when the item itself is later saved.

    It also does not occur when the attachment is saved on the hard disk using the SaveAsFile method.

    Friday, March 20, 2015 10:27 AM
  • Eugene,

    Damian said :

    >Before opening or saving an attachment I need to do some validation. Therefore I have bound to the >following events,

    ((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)mailItem).BeforeAttachmentPreview
    ((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)mailItem).BeforeAttachmentRead

    >These trigger when the attachment is,
    >=> Selected by clicking on it,
    >=> Selecting "Open" and "Save As" from the attachment context menu, shown when right clicked on the >attachment.
    >=> And when "Open" is selected from the above "Opening Mail Attachment" dialog.

    That is why I suggested he consider using those events as an alternative.

    Friday, March 20, 2015 10:28 AM
  • But they are not fired in case of double-cliking on the attachments and choosing the Save option.
    Friday, March 20, 2015 12:00 PM
  • >But they are not fired in case of double-cliking on the attachments and choosing the Save option.

    Which Damian noted after I posted the suggestion.  But thank you for repeating his observation.

    Friday, March 20, 2015 12:21 PM
  • I did a post where I mentioned all the events fired.
    Friday, March 20, 2015 2:13 PM
  • Hi all,

    Thanks for your comments. After some research I found a workaround, this may helps someone.

    When the addin loads the following is done to click on the Open button of the "Opening Mail Attachment" dialog so that the attachment is opened when double clicked on it.

    The following only considers one inspector window, to support multiple, each inspector may need to be stored, bound to the Deactivate and released when the Inspector is closed, binding to the Application.ActiveExplorer().Close.

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
    
    [DllImport("user32.dll", SetLastError = true)]
    public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
    
    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, String lpszClass, String lpszWindow);
    
    Application.ActiveExplorer().Deactivate += OnWindowDeactivate;
    Application.Inspectors.NewInspector += OnNewInspectorWindow;
    
    public void OnNewInspectorWindow(Inspector inspector)
    {
      inspector.Deactivate += OnWindowDeactivate;
    }
    
    public void OnWindowDeactivate()
    {
      IntPtr hwndScreen = IntPtr.Zero;
      hwndScreen = FindWindow((string)null, "Opening Mail Attachment");
      if (hwndScreen != IntPtr.Zero)
      {
        const int nChars = 256;
        System.Text.StringBuilder Buff = new System.Text.StringBuilder(nChars);
        uint BN_CLICKED = 245;
        IntPtr hwndScreen2 = IntPtr.Zero;
        hwndScreen2 = FindWindowEx(hwndScreen, IntPtr.Zero, (string)null, "&Open");
        SendMessage(hwndScreen2, BN_CLICKED, 0, 0);
      }
    }
    

    Wednesday, March 25, 2015 9:59 AM