Home > OS >  Outlook VSTO Add-in how to get reference to a user initiated new appointment
Outlook VSTO Add-in how to get reference to a user initiated new appointment

Time:04-10

I am working on an outlook add-in that will save customer userProperties to an outlook appointment. I am able to get reference to an existing appointment using the selectionChange event.

public partial class ThisAddIn
{
    Outlook.Explorer currentExplorer = null;
    private Outlook.AppointmentItem appointmentItem = null;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        currentExplorer = this.Application.ActiveExplorer();
        currentExplorer.SelectionChange  = new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
   
    }


    private void CurrentExplorer_Event()
    {  

        String expMessage = "default";
        try
        {
            if (this.Application.ActiveExplorer().Selection.Count > 0)
            {
                Object selObject = this.Application.ActiveExplorer().Selection[1];
                if (selObject is Outlook.AppointmentItem)
                {
                    appointmentItem = (selObject as Outlook.AppointmentItem);
                    expMessage = "The item is an appointment. The subject is "   appointmentItem.Subject   ".";
                    MessageBox.Show(expMessage);
                }
            }
        }
        catch (Exception ex)
        {
            expMessage = "Error happened "    ex.Message;
        }
        
    }

However when a user initiates a new appointment, the this.Application.ActiveExplorer().Selection.Count is 0.

I wanted to see how we can get a reference to on Outlook AppointmentItem that was triggered by a user and not been saved yet

CodePudding user response:

This is how I was able to figure the solution. The above code works fine when an existing appointment is opened.

For new appointment, you can use the ActiveInspector to get reference to the new AppointmentItem

Outlook.AppointmentItem appointment = null;
            Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
            if (inspector.CurrentItem is Outlook.AppointmentItem)
                appointment = inspector.CurrentItem as Outlook.AppointmentItem;

CodePudding user response:

You can use either Inspectors.NewInspector event when new new appointment is displayed or Items.ItemAdd event on the MAPIFolder corresponding to Calendar folder after the new appointment is saved.

CodePudding user response:

You are trying to mix different Outlook windows (inspectors and explorers) to get items there. If you need to get an item when a user creates a new appointment item you need to handle the NewInspector event which is fired whenever a new inspector window is opened, either as a result of user action or through program code. You can differentiate existing items and new ones by checking the EntryID property which returns an empty string in case of new items. The EntryID property value is set when an item is saved to the store.

So, there is no need to handle the SelectionChange event which is fired when the user selects a different or additional Microsoft Outlook item programmatically or by interacting with the user interface. This event also occurs when the user (either programmatically or via the user interface) clicks or switches to a different folder that contains items, because Outlook automatically selects the first item in that folder.

class Connect
{
    // Connect class-level Instance Variables
    // Outlook inspectors collection
    private Outlook.Inspectors inspectors;

    public Connect(Outlook.Inspectors Inspectors)
    {
        inspectors = Inspectors;
        
        // Hook up NewInspector event
        inspectors.NewInspector  = new 
            Outlook.InspectorsEvents_NewInspectorEventHandler(
                inspectors_NewInspector);
    }

    // NewInspector event creates new instance of OutlookInspector
    void inspectors_NewInspector(Outlook.Inspector Inspector)
    {
        // use the CurrentItem to get the item object
        // inspector.CurrentItem
    }
 
}

So, there is no need to use the ActiveInspector property.

You may find the Implement a wrapper for inspectors and track item-level events in each inspector article helpful.

  • Related