none
How to catch adding attachments by user? RRS feed

  • Question

  • Hi all!

    I need to realize next scenario in Outlook.

    1) User in outlook creates new letter.

    2) User clicks "attach file", chooses files.

    At this moment I want my program (generally it is in tray) to get control, check something about this files and either really add them

    or add some my file(s) instead (generally i am going to compress files that user add in archive).

    3) User continues his work.


    I have found different sources, like this http://msdn.microsoft.com/en-us/library/aa209975(v=office.11).aspx

    but as i understand it works with mails, created by me.

    Thank you in advance,

    Michael

    Thursday, April 26, 2012 10:58 AM

Answers

  • there is http://msdn.microsoft.com/en-us/library/bb147895(v=office.12).aspx (before attachment add). If your app is one outside of outlook then you need to grab its instance by Marshal.GetActiveObject, cast it to outlook's Application and listen for NewInspector event. There check if this is inspector for new mail item and attach to that event described above.
    • Marked as answer by Mikaell_A Tuesday, May 15, 2012 8:46 AM
    Thursday, April 26, 2012 12:02 PM
  • Attachment type you usually can figure out from the file name extension, or from PR_ATTACH_MIME_TAG_W ( for PropertyAccessor that is "http://schemas.microsoft.com/mapi/proptag/0x370E001F").
     
    The path is not stored in the item for security reasons, that information in the KB article is not accurate. Just check the PathName property to verify that.
     
    You can save the item to a file or stream but your ability to work with the item depends on your being able to handle that type of file, and recognizing the type of file.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Mikaell_A" <=?utf-8?B?TWlrYWVsbF9B?=> wrote in message news:eebc1414-5192-40ad-bd66-b194ca5c4fa5...

    Do you mean that I will not be able to understand that user selected files of some type (for example JPEG)?

    And will not be able to read them or read data that they contain?

    You have written "have to be saved to the file system or a stream", will I be able to do it?

    Here FileName and PathName are mentioned http://msdn.microsoft.com/en-us/library/bb176633(v=office.12).aspx

    Thank you for your answer.


    Ken Slovak MVP - Outlook
    Thursday, April 26, 2012 2:31 PM
    Moderator

All replies

  • there is http://msdn.microsoft.com/en-us/library/bb147895(v=office.12).aspx (before attachment add). If your app is one outside of outlook then you need to grab its instance by Marshal.GetActiveObject, cast it to outlook's Application and listen for NewInspector event. There check if this is inspector for new mail item and attach to that event described above.
    • Marked as answer by Mikaell_A Tuesday, May 15, 2012 8:46 AM
    Thursday, April 26, 2012 12:02 PM
  • Just bear in mind that when that event fires the Attachment object that is passed to the event handler does not provide information about the original file path for the attachment, all you can do is allow or cancel the attach action. To do any manipulation of the Attachment object it would have to be saved to the file system or a stream to work with it.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "DamianD" <=?utf-8?B?RGFtaWFuRA==?=> wrote in message news:dc6758b2-4f6b-4dc1-83cd-7f2d67e0675e...
    there is http://msdn.microsoft.com/en-us/library/bb147895(v=office.12).aspx (before attachment add). If your app is one outside of outlook then you need to grab its instance by Marshal.GetActiveObject, cast it to outlook's Application and listen for NewInspector event. There check if this is inspector for new mail item and attach to that event described above.

    Ken Slovak MVP - Outlook
    Thursday, April 26, 2012 2:13 PM
    Moderator
  • Do you mean that I will not be able to understand that user selected files of some type (for example JPEG)?

    And will not be able to read them or read data that they contain?

    You have written "have to be saved to the file system or a stream", will I be able to do it?

    Here FileName and PathName are mentioned http://msdn.microsoft.com/en-us/library/bb176633(v=office.12).aspx

    Thank you for your answer.

    Thursday, April 26, 2012 2:23 PM
  • Attachment type you usually can figure out from the file name extension, or from PR_ATTACH_MIME_TAG_W ( for PropertyAccessor that is "http://schemas.microsoft.com/mapi/proptag/0x370E001F").
     
    The path is not stored in the item for security reasons, that information in the KB article is not accurate. Just check the PathName property to verify that.
     
    You can save the item to a file or stream but your ability to work with the item depends on your being able to handle that type of file, and recognizing the type of file.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Mikaell_A" <=?utf-8?B?TWlrYWVsbF9B?=> wrote in message news:eebc1414-5192-40ad-bd66-b194ca5c4fa5...

    Do you mean that I will not be able to understand that user selected files of some type (for example JPEG)?

    And will not be able to read them or read data that they contain?

    You have written "have to be saved to the file system or a stream", will I be able to do it?

    Here FileName and PathName are mentioned http://msdn.microsoft.com/en-us/library/bb176633(v=office.12).aspx

    Thank you for your answer.


    Ken Slovak MVP - Outlook
    Thursday, April 26, 2012 2:31 PM
    Moderator
  • Thank you for your answer. Finally, I've got through that way.

    In the end I have only one little problem: event BeforeAttaqchmentAdd occurs for each file added as attachment.

    So, if I add two files together I have two events.

    If I want to catch all events of attachment and than to do my operations that is OK,

    but if I want to separate several attacments adding to one mail I have a problem.

    Is there any solution?

    Tuesday, May 15, 2012 9:44 AM
  • could you elaborate more? i;m not sure i understand 'I want to separate several attacments adding to one mail I have a problem'
    Tuesday, May 15, 2012 10:07 AM
  • Sorry for my english(

    Example:

    1) I opened new mail. Pressed "Attach file". Selected two files.

    Than I receive two BeforeAttaqchmentAdd events for each file.

    2) Than I press "Attach file" again. Selected another two files.

    I receive again two BeforeAttaqchmentAdd events for each new file.

    Can I somehow say that first two events were set by one occurrence of attaching files,

    and other by second?

    As I had list of all files atached durins one "Attach file"?

    Tuesday, May 15, 2012 10:21 AM
  • no, there is no way to control how and when OOM raises events. You get them as they come. If you want somehow to store some state to share it between events, you could alsways use mailItem.UserProperty to persist it accoros events and clear that state for example on ItemSend event
    Tuesday, May 15, 2012 10:33 AM
  • I'll try think about this way more, but yet I've not found how can I save this state.

    Also, I tried from other angle, but also haven't found a solution:

    is there  any event of pressing "attach file", but before selecting files? I've not found such event in MSDN.

    Tuesday, May 15, 2012 5:34 PM
  • There's no such Outlook event, but you can catch a ribbon button click for that and any other "attach" button. You'd repurpose the ribbon command and handle its click event, then you can cancel or allow the original click handler.
     
    See http://msdn.microsoft.com/en-us/library/bb462633(v=office.12).aspx for information about repurposing ribbon controls.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Mikaell_A" <=?utf-8?B?TWlrYWVsbF9B?=> wrote in message news:558ab567-42bd-4c60-b590-4fb90701a1a7...

    I'll try think about this way more, but yet I've not found how can I save this state.

    Also, I tried from other angle, but also haven't found a solution:

    is there  any event of pressing "attach file", but before selecting files? I've not found such event in MSDN.


    Ken Slovak MVP - Outlook
    Tuesday, May 15, 2012 5:49 PM
    Moderator