Search code examples
vstooutlook-addin

VSTO Addin item related variable & calling item identification


I am working on an Outlook Addin related to calendar appointment. My problem is that I need to save the initial appointment title and then check when saving if it was updated to trigger additional actions. I have understood that there is only one single instance of the Addin/Ribbon. I initially used static variable but it does not help as when multiple windows are opened then title get mixed up. My questions are: 1) Where to store variables related to an appointment? 2) How to detect which object has been calling the write/save/send/ methods?

Here is a relevant extract of my code:

public partial class ThisAddIn
{

    public static Outlook.AppointmentItem appointmentItem;
    public static Addin_Ribbon ribbon;
    Outlook.Inspectors inspectors;

// Needs to be saved for each appointments
public static string initialMeetingSubject = "";

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        inspectors = this.Application.Inspectors;
        inspectors.NewInspector +=
        new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    }

    protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
    {
        ribbon = new MyAddin_Ribbon();
        return ribbon;
    }

    void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
    {
        // Save current appointment reference
        appointmentItem = Inspector.CurrentItem as Outlook.AppointmentItem;

        if (appointmentItem != null)
        {
            (appointmentItem as Microsoft.Office.Interop.Outlook.ItemEvents_10_Event).Send += _appointment_Send;
            (appointmentItem as Microsoft.Office.Interop.Outlook.ItemEvents_10_Event).BeforeDelete += _appointment_Delete;
            (appointmentItem as Microsoft.Office.Interop.Outlook.ItemEvents_10_Event).Write += _appointment_Write;    
            (appointmentItem as Microsoft.Office.Interop.Outlook.ItemEvents_10_Event).Open += _appointment_Open;
            (appointmentItem as Microsoft.Office.Interop.Outlook.ItemEvents_10_Event).Close += _appointment_Close;

            // Save initial value to compare during appointment save
            ThisAddIn.initialMeetingSubject = appointmentItem.Subject;

        }
    }

    private void _appointment_Write(ref bool Cancel)
    {
        Logger.WriteLine(LogLevel.Debug, "Appointment WRITE Initial Subject: " +initialMeetingSubject + “ Updated Subject: “ + ThisAddIn.appointmentItem.Subject);
    }
}

Any assistance will be much appreciated.


Solution

  • Ok Microsoft tutorial that I posted in my previous comment is actually pretty clear and efficient. Following the instruction, I created a wrapper class for appointment items and it worked out like a charm. A test project can be downloaded here