Implement a wrapper for inspectors and track item-level events in each inspector

This topic contains two code examples that show how to implement a wrapper for an Inspectors collection and to use that wrapper to track item-level events in each Inspector object in the collection.

Example

Note

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.

The following two code examples implement the Connect and OutlookInspector classes. The first code example involves methods and event handlers you include in the Connect class to implement a wrapper for an Inspectors collection. The second code example involves a simple implementation of the OutlookInspector class.

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;

In the following code example, a NewInspector(Inspector) event occurs after a new inspector window has been created and before it is displayed. A user action may also create a new inspector window. A class-level instance variable named inspectors in the Connect class is declared, and a NewInspector event is hooked up.

In the inspectors_NewInspector method, the FindOutlookInspector method checks whether the new inspector window is already in the inspectorWindows list. If FindOutlookInspector does not find the Inspector object in inspectorWindows, the AddInspector method adds an instance of the OutlookInspector class to inspectorWindows. You can use the OutlookInspector class to raise events for this particular inspector window. The implementation of the OutlookInspector class is shown in the second code example.

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

    // Collection of tracked inspector windows              
    private List<OutlookInspector> inspectorWindows;   
    
    public Connect(Outlook.Inspectors Inspectors)
    {
        inspectors = Inspectors;
        inspectorWindows = new List<OutlookInspector>();
        // 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)
    {
        // Check to see if this is a new window you don't
        // already track
        OutlookInspector existingWindow = 
            FindOutlookInspector(Inspector);
        if (existingWindow == null)
        {
            AddInspector(Inspector);
        }
    }

    // Adds an instance of **OutlookInspector** class
    private void AddInspector(Outlook.Inspector inspector)
    {
        OutlookInspector window = new OutlookInspector(inspector);
        window.Close +=
            new EventHandler(WrappedInspectorWindow_Close);
    }

    // Looks up the window wrapper for a given Inspector 
    // window object
    private OutlookInspector FindOutlookInspector(object window)
    {
        foreach (OutlookInspector inspector in inspectorWindows)
        {
            if (inspector.Window == window)
            {
                return inspector;
            }
        }
        return null;
    }
}

The following code example is an implementation of the OutlookInspector class. This class is used to raise events for the inspector window from the preceding code example. Multiple inspector windows can be open simultaneously. Item-level events such as Open, PropertyChange, and CustomPropertyChange are tracked by hooking them up in this class constructor. A Close event for a ContactItem object is also hooked up in this class constructor. You can define other class-level item instance variables as needed. All the events that were hooked up in the OutlookInspector constructor are unhooked in the OutlookInspectorWindow_Close event handler.

Note that at the object model level, an Outlook inspector object is not specific to any Outlook item type. This code sample makes use of the OutlookItem helper class, defined in Create a Helper Class to Access Common Outlook Item Members, to conveniently call the OutlookItem.Class property to verify the message class of the current item in the inspector, before assuming the item is a contact item.

// This class tracks the state of an Outlook Inspector window 
// and ensures that what happens in this window is handled correctly.
class OutlookInspector
{
    // OutlookInspector class-level instance variables 
    // wrapped window object
    private Outlook.Inspector m_Window;             

    // Use these instance variables to handle item-level events
    // wrapped MailItem
    private Outlook.MailItem m_Mail;    
    // wrapped AppointmentItem        
    private Outlook.AppointmentItem m_Appointment;  
    // wrapped ContactItem
    private Outlook.ContactItem m_Contact;
    // wrapped TaskItem      
    private Outlook.TaskItem m_Task;             

    // OutlookInspector constructor
    public OutlookInspector(Outlook.Inspector inspector)
    {
        m_Window = inspector;

        // Hook up the close event
        ((Outlook.InspectorEvents_Event)inspector).Close +=
            new Outlook.InspectorEvents_CloseEventHandler(
            OutlookInspectorWindow_Close);

        // Hook up item-level events as needed
        OutlookItem olItem = new OutlookItem(inspector.CurrentItem);
        if(olItem.Class==Outlook.OlObjectClass.olContact)
        {
            m_Contact = olItem.InnerObject as Outlook.ContactItem;
            m_Contact.Open +=
                new Outlook.ItemEvents_10_OpenEventHandler(
                m_Contact_Open);
            m_Contact.PropertyChange +=
                new Outlook.ItemEvents_10_PropertyChangeEventHandler(
                m_Contact_PropertyChange);
            m_Contact.CustomPropertyChange +=
                new Outlook.ItemEvents_10_CustomPropertyChangeEventHandler(
                m_Contact_CustomPropertyChange);
        }
    }

    // Event Handler for the inspector close event.
    private void OutlookInspectorWindow_Close()
    {
        // Unhook events from any item-level instance variables
        m_Contact.Open -= 
            Outlook.ItemEvents_10_OpenEventHandler(
            m_Contact_Open);
        m_Contact.PropertyChange -= 
            Outlook.ItemEvents_10_PropertyChangeEventHandler(
            m_Contact_PropertyChange);
        m_Contact.CustomPropertyChange -= 
            Outlook.ItemEvents_10_CustomPropertyChangeEventHandler(
            m_Contact_CustomPropertyChange);
        ((Outlook.ItemEvents_Event)m_Contact).Close -= 
            Outlook.ItemEvents_CloseEventHandler(
            m_Contact_Close);

        // Unhook events from the window
        ((Outlook.InspectorEvents_Event)m_Window).Close -=
            new Outlook.InspectorEvents_CloseEventHandler(
            OutlookInspectorWindow_Close);

        // Raise the OutlookInspector close event
        if (Close != null)
        {
            Close(this, EventArgs.Empty);
        }
        // Release item-level instance variables
        m_Mail = null;
        m_Appointment = null;
        m_Contact = null;
        m_Task = null;
        m_Window = null;
    }
}

See also