none
Write Event is not firing with recurring appointment RRS feed

  • Question

  • Hi,

    I have an outlook addin. In it I'm checking the appointment is recurring or not. If appoint is recurring, i am initialize the "write" event  for each occurring of appointment. I am pasting the code below.

    [Code]

     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Xml.Linq;

    using Outlook = Microsoft.Office.Interop.Outlook;

    using Office = Microsoft.Office.Core;

     

    namespace RecurringAppointments

    {

        public partial class ThisAddIn

        {

            Outlook.Explorer OutllookExplorer;

            Outlook.AppointmentItem CurrentMeeting;

            Outlook.Application OutlookApplication;

     

            private void ThisAddIn_Startup(object sender, System.EventArgs e)

            {

                OutlookApplication = (Outlook.Application)this.Application;

                OutllookExplorer = (Outlook.Explorer)OutlookApplication.ActiveExplorer();

                OutllookExplorer.FolderSwitch += new Outlook.ExplorerEvents_10_FolderSwitchEventHandler(OutllookExplorer_FolderSwitch);

            }

     

     

    private void OutllookExplorer_FolderSwitch()

            {

              Outlook.MAPIFolder folder = OutllookExplorer.CurrentFolder;

                if (folder.DefaultItemType == Outlook.OlItemType.olAppointmentItem)

                {

                    foreach (object item in folder.Items)

                    {

                        Microsoft.Office.Interop.Outlook.AppointmentItem it = (Microsoft.Office.Interop.Outlook.AppointmentItem)item;

                         if (it.IsRecurring == true)

                            FindRecurring(it.EntryID, it.Start);

                    }

                }

            }

     

    private void FindRecurring(string entryID, DateTime StartDate)

            {

                Outlook.MAPIFolder folder = OutllookExplorer.CurrentFolder;

                Outlook.Items AllAppointments;

                string filter1 = "[Start] <= '" + StartDate.ToString() + "'";

                AllAppointments = folder.Items;

                AllAppointments.Sort("[Start]");

                AllAppointments.IncludeRecurrences = true;

                Outlook.AppointmentItem RecurringItem = AllAppointments.Find(filter1);

                object myMissing = System.Reflection.Missing.Value;

                int i = 1;

                SugarOutlook.DB.SQLiteDB objDB = new SugarOutlook.DB.SQLiteDB();

                while (RecurringItem != null)

                {

                    if (entryID == RecurringItem.EntryID)

                    {

                        //RecurringItem.Open += new Outlook.ItemEvents_10_OpenEventHandler(RecurringItem_Open);

                        RecurringItem.Write += new Outlook.ItemEvents_10_WriteEventHandler(RecurringItem_Write);

                    }

                    RecurringItem = AllAppointments.FindNext();

                }

            }

     

    private void RecurringItem_Write(ref bool Cancel)

            {

                object item = OutlookApplication.ActiveInspector().CurrentItem;

                if (item is Outlook.AppointmentItem)

                {

                    Outlook.AppointmentItem CurrentMeeting = (Outlook.AppointmentItem)item;

                    System.Windows.Forms.MessageBox.Show(CurrentMeeting.Body.ToString());

                }

            }

     

        }

    }

    [/Code]

     

    this function is triggering private void RecurringItem_Write(ref bool Cancel)... I am not getting any error also

    Thursday, December 29, 2011 2:50 PM

Answers

  • 2 issues:

    - i;m not sure if getting items by means of 'IncludeRecurrences' returns you exactly the same items that outlook will use later for edition of single occurances in recurring event, maybe they are just 'virtual' items, and outlook uses other 'virtual' item when user opens single occurence, no idea, you would have to check if, or some outlook expert would have to chime in on this

    - even if problem above does not exist (outlook returns the same items that it uses later for open/read in inspector) there is still a problem with your code. You are iterating through collection of appointments in items and attaching to Open & Write events but you are not storing those items in any collection/variable set , only last one after you exit your loop. It will simply not work, first memory sweep of GC will collect all those pending appointment items and your event handlers attached to those items will not fire anymore.

    Tuesday, January 3, 2012 8:20 AM
  • Hi Bobbin,

    As Damian stated, instances of recurring appointments are not "real" items as far as the object model is concerned. The only item is the AppointmentItem for the master appointment, so that would be the only item where the Write event would apply. You can determine when the something has changed on the master appointment by using the Write event, but you would need to add your own logic to figure out which instance changed by checking out the corresponding exceptions (http://msdn.microsoft.com/en-us/library/ff860328.aspx).


    Bill Jacob - Microsoft Customer Service & Support - Developer Messaging
    Tuesday, January 3, 2012 4:03 PM
    Moderator

All replies

  • Hi Bobbin,

     

    Thanks for posting in the MSDN Forum.

     

    The statement to determine “if(entryID == RecurringItem.EntryID)” have problem. It’s based on my understanding that statement is: if variable entryID’s pointer has same memory address of variable RecurringItem’s EntryID property. It’s obviously not. So your will not attach any event to your recurring appointment. I would recommend you use “if(entryID.Equals(RecurringItem.EntryID))” instead of that statement to determine.

     

    I hope what I said can help you.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 2, 2012 5:14 AM
    Moderator
  • Hi Tom,

     

    Thanks for your reply.

    I tried with the change you specified. Its working only one time. I mean if i edit and save the first occurring item from the recurring appointment item, the event will work.  if i edit and save the other occurring items from the same recurring appointment, the event is not triggering.

     

    Thanks 

    Bobbin Paulose

    Monday, January 2, 2012 12:06 PM
  • First a thing about == and equals - in outlook OOM EntryID is simply a string, which has overload on == to do 'string' comparision, not something along the object.ReferenceEquals.

     

    Now to your problem - when you attach for event on some class, you have to hold onto that variable, otherwise next GC will collect that class and your event will not fire anymore. You are iterating through collection but i do not see code that keeps all appointment items that you attached event to in some collection.

    Monday, January 2, 2012 2:08 PM
  • Hi Damian

     

    My aim is to the changed occurrence item from the Recurring appointment. 

    Eg: I have a recurring item and it have 5 occurrence  from Jan 1st to Jan 5th. If i edit the body of 4th occurrence, I need to get the date of that occurrence. 

    Is there any method to solve this problem. I am trying to fix this problem for last 1 week. 

     

    Thanks

    Bobbin

    Monday, January 2, 2012 4:12 PM
  • Hi Bobbin & DamianD,

     

    I think Bobbin’s snippet will attach the event to all of the appointment items except recurring items if he didn’t set the condition to check whether it is a recurring item. I will consult some experts for it to see whether it can be fix. There might be some time delay. Appreciate for your patience.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 3, 2012 8:00 AM
    Moderator
  • 2 issues:

    - i;m not sure if getting items by means of 'IncludeRecurrences' returns you exactly the same items that outlook will use later for edition of single occurances in recurring event, maybe they are just 'virtual' items, and outlook uses other 'virtual' item when user opens single occurence, no idea, you would have to check if, or some outlook expert would have to chime in on this

    - even if problem above does not exist (outlook returns the same items that it uses later for open/read in inspector) there is still a problem with your code. You are iterating through collection of appointments in items and attaching to Open & Write events but you are not storing those items in any collection/variable set , only last one after you exit your loop. It will simply not work, first memory sweep of GC will collect all those pending appointment items and your event handlers attached to those items will not fire anymore.

    Tuesday, January 3, 2012 8:20 AM
  • Hi Bobbin,

    As Damian stated, instances of recurring appointments are not "real" items as far as the object model is concerned. The only item is the AppointmentItem for the master appointment, so that would be the only item where the Write event would apply. You can determine when the something has changed on the master appointment by using the Write event, but you would need to add your own logic to figure out which instance changed by checking out the corresponding exceptions (http://msdn.microsoft.com/en-us/library/ff860328.aspx).


    Bill Jacob - Microsoft Customer Service & Support - Developer Messaging
    Tuesday, January 3, 2012 4:03 PM
    Moderator
  • Hi

    Thanks for all the above replies.....

    I tried this code to find the master appointment item from the recurring items

     

    private void FindRecurring(string entryID, DateTime StartDate)

            {

                Outlook.MAPIFolder folder = OutllookExplorer.CurrentFolder;

                Outlook.Items AllAppointments;

                string filter1 = "[Start] <= '" + StartDate.ToString() + "'";

                AllAppointments = folder.Items;

                AllAppointments.Sort("[Start]");

                AllAppointments.IncludeRecurrences = true;

                Outlook.AppointmentItem RecurringItem = AllAppointments.Find(filter1);

                while (RecurringItem != null)

                {

                    if (RecurringItem.RecurrenceState == Outlook.OlRecurrenceState.olApptMaster)

                         System.Windows.Forms.MessageBox.Show(RecurringItem.Subject.ToString() + " : Master");

                    else if (RecurringItem.RecurrenceState == Outlook.OlRecurrenceState.olApptOccurrence)

                        System.Windows.Forms.MessageBox.Show(RecurringItem.Subject.ToString() + " : Occurance");

                    RecurringItem = AllAppointments.FindNext();

                }

            }

     

    It always entering the else if conditions [Outlook.OlRecurrenceState.olApptOccurrence].

    Wednesday, January 4, 2012 11:33 AM
  • You can always get master item by using 'Parent' property of appointment, but it will change nothing in your quest for happiness - single occurance items do not exist untill user edits single occurance and changes something. What you see in calendar view is just a trick. You have to think of another way to make sure your code is called when user edits single occurance.

    Wednesday, January 4, 2012 12:34 PM
  • Bobbin, did you need any further assistance with this? Overall, I hope the responses you recieved so far were helpful, since in many ways this is really an "architectural-type" question about Outlook and it's object model. So if there's anything else I can clarify, please let me know.

    I guess the important thing to try to keep in mind is that the key thing is the master appointment. As Damian stated, that is the only "real" item in the folder, and all object model events need to be hooked up based on that master item, not any of the occurences.


    Bill Jacob - Microsoft Customer Service & Support - Developer Messaging
    Tuesday, January 10, 2012 11:21 PM
    Moderator
  • Hi

    Thanks for all reply's. Anyway i achieved my goal with this thread. Thank you to all my friends

    Bobbin 

    Monday, April 23, 2012 9:30 AM
  • glad to hear that :-)

    Please close the thread by marking answer.

    Monday, April 23, 2012 9:53 AM