none
How to capture a save & saveAs mail item event in c#? RRS feed

  • Question

  • How to capture a save & saveAs mail item event in c#?

    I am using outlook 2007.

    • Edited by Amit m Monday, January 6, 2014 6:35 PM
    Monday, January 6, 2014 6:33 PM

Answers

  • Save: use MailItem.Write event

    SaveAs: there is no that fires when SaveAs is called.


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

    Monday, January 6, 2014 6:38 PM
  • NewInspector() is not the best place to add handlers like that for the item, you're better off doing it in the first Inspector.Activate() event.

    Also, are you setting an object for the item that has scope during the entire lifetime of the item? If not the odds are your handlers are going out of scope and being garbage collected. Show the code where you set up the handlers.


    Ken Slovak MVP - Outlook

    Monday, January 6, 2014 7:14 PM

All replies

  • Save: use MailItem.Write event

    SaveAs: there is no that fires when SaveAs is called.


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

    Monday, January 6, 2014 6:38 PM
  • I am already handling write event but event not fire when click on Save button.

    I am clicking on Floppy button to save the mail but not fire can, you tell me

    How can i handle the save/saveas event?

    I am doing this in windows application.
    • Edited by Amit m Monday, January 6, 2014 6:52 PM
    Monday, January 6, 2014 6:47 PM
  • Are you sure you set up the event handler correctly? If I open a message in Outlook, modify it, then click on the CurrentItem button on the OutlookSpy toolbar and go to the Events tab, I can see the following events raised when I click on the Save button:

    PropertyChange (ReceivedTime)
    Write (false)
    BeforeCheckNames (false)
    AfterWrite ()
    <Unknown(DispID=0x0000FC95)> (false)


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

    Monday, January 6, 2014 6:52 PM
  • Yes i did, Let me explain you what i did.

    I have outlook 2007. Double click on a mail item Then NewInspector Event execute, Here i am attaching 

    Open and Write event both. I can not do editing the message becuase outlook is not allowing me to edit the

    message here. so i did not  edit mail content.  Now i clicked on Save button, Then event does not fire.

    How can i handle save/saveas event both?

    Yes, But when i edit the content and save then it fire the write event.
    • Edited by Amit m Monday, January 6, 2014 7:04 PM
    Monday, January 6, 2014 7:03 PM
  • Hello Amit,

    Instead, you can intercept the Save and SaveAs buttons from the Outlook UI. You can try to use the and FileSave and FileSaveAs IDs correspondingly. Read more about this in the Customizing the 2007 Office Fluent Ribbon for Developers article in MSDN.

    Monday, January 6, 2014 7:03 PM
  • Is it possible through c# coding?

    Is there any small tutorial to solve my issue in c#?

    I want to do it through windows application?

    Monday, January 6, 2014 7:12 PM
  • NewInspector() is not the best place to add handlers like that for the item, you're better off doing it in the first Inspector.Activate() event.

    Also, are you setting an object for the item that has scope during the entire lifetime of the item? If not the odds are your handlers are going out of scope and being garbage collected. Show the code where you set up the handlers.


    Ken Slovak MVP - Outlook

    Monday, January 6, 2014 7:14 PM
  • However, that wouldn't handle other methods of saving items such as autosave for drafts or any context menu clicks for example.

    Ken Slovak MVP - Outlook

    Monday, January 6, 2014 7:16 PM
  • If you develop a standalone application, you have to use methods, properties and events that the Outlook Object Model provides. So, intercepting buttons from the UI is not an option.
    Monday, January 6, 2014 7:16 PM
  • If you do make any changes, the Save button or the MailItem.Save method will do nothing, hence the event Write event will 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!

    Monday, January 6, 2014 7:50 PM
  • How to use Inspector.Activate() event to handle save/saveas event?

    Please help i do not have much experience in outlook.

    Tuesday, January 7, 2014 10:14 AM
  • Hi Amit,

    You can subscribe to the Activate event of the Incpector class in the following way:

    private void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector
    Inspector)
    {
       ((Microsoft.Office.Interop.Outlook.InspectorEvents_Event)Inspector).Activate += new 
    Microsoft.Office.Interop.Outlook.InspectorEvents_ActivateEventHandler(Inspector_Activate);
    
    }
    

    Also please take a look at the Developing an Inspector Wrapper for Outlook 2010 article in MSDN.

    In the Activate event handler you can subscribe to the Write event of the MailItem class.

    Tuesday, January 7, 2014 12:51 PM
  • Many thanks for reply. I have outlook 2007 not 2010.

    Can you tell me, How can i handle FileSave/FileSaveAs event of mailitem?

    Wednesday, January 8, 2014 11:38 AM
  • Hi Amit,

    The MailItem class doesn't provide such events. Instead, you can use the Write or AfterWrite events as Dmitry suggested. You can use only these events if you develop a standalone windows forms application which automates Outlook.

    Also, if you develop a COM add-in or VBA macro, as I wrote you earlier, you can intercept the FileSave and FileSaveAs buttons from the Outlook UI. You can try to use the and FileSave and FileSaveAs IDs correspondingly. Read more about this in the Customizing the 2007 Office Fluent Ribbon for Developers article in MSDN. For example, the following markup can be used for intercepting the mentioned buttons:

    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="LoadedCallback" loadImage="ImagesCallback">
      <commands>
        <command getEnabled="getEnabled_Callback" idMso="FileSave" onAction="SaveCallback" />
        <command getEnabled="getEnabled_Callback" idMso="FileSaveAs" onAction="SaveAsCallback" />
      </commands>
    </customUI>
    Note, standalone applications don't provide an easy way for intercepting UI controls.


    Wednesday, January 8, 2014 1:59 PM
  • However, a Windows application cannot intercept ribbon events. That would require an Outlook COM addin. From a Windows application, as noted previously, only the Outlook object model could be used.

    Ken Slovak MVP - Outlook

    Wednesday, January 8, 2014 3:45 PM
  • Ken,

    Why did you decide that a Windows application is used?

    There were no mentions. Here is what I see in the initial post:

    > I am using outlook 2007.

    Wednesday, January 8, 2014 4:07 PM
  • The post in this thread on January 6, at 6:52 PM has this line:

    I am doing this in windows application.

    That indicates to me that a COM addin is not being used and the code is running out-of-process to Outlook. Therefore ribbon handling can't be implemented.


    Ken Slovak MVP - Outlook

    Wednesday, January 8, 2014 4:27 PM
  • Thank you. It is hard to find a key point in such chaos of messages.
    Wednesday, January 8, 2014 4:41 PM
  • Thanks Mr Eugene for your great contribution.

    My Simple question is that how can i handle outlook mail save/saveas event in c# window application, I have added outlook reference in my project?

    I am using VS-2005.



    • Edited by Amit m Thursday, January 9, 2014 8:41 AM
    Thursday, January 9, 2014 5:05 AM
  • I have attached open/write event at activate, but event does not fire when open/save. 
    • Edited by Amit m Thursday, January 9, 2014 8:39 AM
    Thursday, January 9, 2014 5:55 AM
  • Was the item actually modified?

    If yes, do you see the event logged in OutlookSpy?


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

    Thursday, January 9, 2014 1:27 PM
  • Hi Amit,

    You may be interested in the How to automate Outlook and Word by using Visual C# .NET to create a pre-populated e-mail message that can be edited article. It describes all the required steps for automating Outlook from a standalone C# application.  

    Thursday, January 9, 2014 1:39 PM
  • Amit,

    What code do you use for subscribing to the Write event?

    Thursday, January 9, 2014 1:40 PM
  • MailItem.write+=writevent in inspector Activation.
    • Edited by Amit m Friday, January 10, 2014 8:54 AM
    Friday, January 10, 2014 5:35 AM
  • This article is good. But but what i am doing let me explain you.

    When i am opening the outlook message from my desk then my c# window application MailItem.Open execute.

    Like this when i click the save/saveas button or from menu then i want to execute a event that say user is saving the mailitem, if user does not edit the content of mailitem then also saveevent should execute.

    Is that possible?

    Friday, January 10, 2014 5:42 AM