none
How to revert changed state of AppointmentItem RRS feed

  • Question

  • Hi, 

    In my Outlook add-in I've changed some property of AppiontmentItem, says add or change an user property and Save property changes. I've also subscribed to Write event and in some situation I've canceled the Save. But, as mentioned in documentation (False when the event occurs. If the event procedure sets this argument to True, the save operation is not completed) it cancels only Save operation but uncommitted state of property remains and Want to save your changes? dialog box appears when we close Outlook.

    Is there any possibility to avoid this situation, ideally to reset the property changed (unsaved) state. I've tried to set property value to its initial value without luck.


    Wednesday, January 27, 2016 8:42 PM

All replies

  • No, once an item is modified, only Save will reset the Modifield property to false.

    You can try to use MailItem.Close(olDiscard) and then reopen the item using Namespace.GetItemfromID, but I don't think this will be practical in all cases.


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

    Wednesday, January 27, 2016 8:55 PM
  • Thank you for reply. I haven't opened any inspector, I just change property, so it is not an option for me. Can I open an inspector in hidden mode?
    Wednesday, January 27, 2016 11:03 PM
  • In that case there won't be a save prompt - why not simply discard (by dereferencing) the item?

    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 28, 2016 4:36 AM
  • What do you mean by dereferencing?
    Thursday, January 28, 2016 5:52 AM
  • Set to null (or Nothing in VB) or (better yet) use Marshal.ReleaseComObject (in case of .Net). I am probably missing something, but what is the exact problem that you are trying to solve? What exactly happens if you don't undo a change?

    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 28, 2016 1:05 PM
  • Here is my scenario:

    I'm using user properties to build my application logic related to appointment items and use custom form opened via context menu of item to interact with user. On some of my calendar items I have been monitoring any changes to inform user that the item will be detached from add-in system in case if changes was made outside the add-in custom form (i.e. inspector or explorer). But when I'm making changes from my custom form and try to save them Write event raises and OK/Cancel dialog box asks to me that item will be detached from add-in system. When I hit Cancel the save cancelled  but the property remains changed (which cause Write event raises) and when user close the Outlook the Want to save your changes? dialog box appears which doesn't inform which changes does it mean. If user hit Yes the the invariant of item's user property system will destroyed.


    Thursday, January 28, 2016 1:46 PM
  • The fact that you get a prompt upon Outlook closing means you are keeping a reference to the modified items. You need to make sure you release all such references. Do you store the references in global/class variables?

    Also, if you don't want to persist a user property on an item, why set it to begin with? Keep a list of the items' entry ids vs the value of your property stored in memory rather than as a user property.


    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 28, 2016 10:56 PM
  • Hello Hamlet,

    When you work with recurring appointment items, you should release any prior references, obtain new references to the recurring appointment item before you access or modify the item, and release these references as soon as you are finished and have saved the changes. This practice applies to the recurring AppointmentItem object, and any Exception or RecurrencePattern object. To release a reference in Visual Basic for Applications (VBA) or Visual Basic, set that existing object to Nothing. In C#, explicitly release the memory for that object. For a code example, see the topic for the AppointmentItem object.

    Note that even after you release your reference and attempt to obtain a new reference, if there is still an active reference, held by another add-in or Outlook, to one of the above objects, your new reference will still point to an out-of-date copy of the object. Therefore, it is important that you release your references as soon as you are finished with the recurring appointment.

    Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.

    Thursday, January 28, 2016 11:31 PM
  • Of course I keep reference to item because I've subscribed to its Write event and I should still monitor item changes if user cancels the prompt to detach item from add-in system.
    Friday, January 29, 2016 12:40 AM

  • Note that even after you release your reference and attempt to obtain a new reference, if there is still an active reference, held by another add-in or Outlook, to one of the above objects, your new reference will still point to an out-of-date copy of the object. 

    That means it is technically impossible to have guarantee that I can avoid save dialog because some 3rd party add-in can hold reference to the item.
    Friday, January 29, 2016 8:55 AM
  • Hi Hamlet,

    Based on the test, the other's add-in wont affect the function in your add-in. As others mentioned, this issue was caused by the reference in your add-in. If you restart the Outlook, you can see the appointment item actually didn't save yet.

    To deference the appointment item, we can initialize it in a class-level and release it in close event of the form region.

    Here is a demo code for your reference:

    partial class FormRegion
    {
    
            Outlook.AppointmentItem appItem;
    
      private void FormRegion2_FormRegionShowing(object sender, System.EventArgs e)
            {
                appItem = (Outlook.AppointmentItem)this.OutlookItem;
                appItem.Write += AppItem_Write;
              }
    
       private void FormRegion1_FormRegionClosed(object sender, System.EventArgs e)
            {
                appItem = null;
            }
    
    
    }
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 1, 2016 7:28 AM
    Moderator
  • I don't understand how this related to my issue?
    Monday, February 1, 2016 5:22 PM
  • Hi,

    Did you refer to the appointment in the form region? The reason for the demo code above is that the issue could only reproduced when I didn't release the appointment item in the form region class.

    If I misunderstood, please feel free to let me know.

    Regards & Fei 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, February 2, 2016 2:37 AM
    Moderator