Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / Office

Outlook 2007 Calendar Preview

0.00/5 (No votes)
20 Mar 2012CPOL1 min read 20.7K   738  
A form region with a calendar preview for outlook 2007

Introduction 

One of the coolest features in Outlook 2010 is the calendar preview. Well, for those of us that are still using Outlook 2007, this is the Calendar Preview.

Image 1

Background

Our company isn't planning to upgrade to Outlook 2010 any time soon. But still I wanted to have this feature on my Outlook. I have a lot of appointments and need to see my conflicts right away. Switching to the calendar to see the conflict and then coming back to the meeting request is such a waste of time. So I decided to write my own Calendar Preview control and place it on the meeting request. For this, I used a Form Region Control and a Calendar Control that I got from this guy. Thank you Ertan Tike.

Using the code

OK, has I said I am using a VSTO project for Outlook 2007 and a form region that is Adjoining? The code is very simple, again thanks to the great control from Ertan Tike that does all the work of showing a calendar like control. 

Let's start with the Form Region Factory part of the form region control. We need to declare what kind of message class we want to use. Because we are dealing with meeting, we need to use this message class: 

C#
[Microsoft.Office.Tools.Outlook.FormRegionMessageClass("IPM.Schedule.Meeting")]

Next, let us look at the FormRegionInitializing event. Here we connect to the Outlook application and to the calendar folder.

C#
private void AppFormRegionFactory_FormRegionInitializing(object sender,
             Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
{
    oApp = new Microsoft.Office.Interop.Outlook.Application();
    oNS = oApp.GetNamespace("MAPI");
    oCalendar = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
}

The next thing is the FormRegionShowing event:

C#
private void AppFormRegion_FormRegionShowing(object sender, System.EventArgs e)
{
    try
    {
        //Get the calendar items
        Microsoft.Office.Interop.Outlook.Items oItems = 
           (Microsoft.Office.Interop.Outlook.Items)oCalendar.Items;  
        //Get the current item
        Microsoft.Office.Interop.Outlook.MeetingItem appM = 
           (Microsoft.Office.Interop.Outlook.MeetingItem)this.OutlookItem;
        //Get it has an Appointment
        Microsoft.Office.Interop.Outlook.AppointmentItem appItem = 
                         appM.GetAssociatedAppointment(false);
        //Set the date and time that we want to show (the Appointment date)          
        string startdate = appItem.Start.Date.ToShortDateString() + " 01:00"; 
        string enddate = appItem.Start.Date.ToShortDateString() + " 23:00";
        //Set the calendar control date
        dayView1.StartDate = appItem.Start.Date;
        dayView1.StartHour = appItem.Start.Hour-2;  //show from 2 hours before
        //Set the filter on our calendar items so we get only today meetings
        string filterTodayAppoints = "([Start]>'" + startdate + 
          "' And [End]<='" + enddate + "')";
        oItems.Sort("[Start]", false);
        oItems.IncludeRecurrences = true;
        myRestricted = oItems.Restrict(filterTodayAppoints);
        m_Appointments = new List<Appointment>();
        foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in myRestricted)
        {
            Appointment m_Appointment = new Appointment();
            m_Appointment.StartDate = item.Start;
            m_Appointment.EndDate = item.End;
            m_Appointment.Title = item.Subject+Environment.NewLine+item.Location+ 
                                  "; " + item.Organizer;
            if (item.Subject == appItem.Subject)
            {                    
                m_Appointment.BorderColor = Color.Orange;
            }
            //Add the to our list
            m_Appointments.Add(m_Appointment);
        }
    }
    catch (Exception ex)
    {
        return;
    }
}

All we got left to do is show our meeting on the calendar control:

C#
private void dayView1_ResolveAppointments(object sender, ResolveAppointmentsEventArgs args)
{
    try
    {
        List<Appointment> m_Apps = new List<Appointment>();
        foreach (Appointment m_App in m_Appointments)
            m_Apps.Add(m_App);

        args.Appointments = m_Apps;
    }
    catch (Exception ex)
    {
        return;
    }
}

That's it. 

Points of Interest 

If you are not familiar with the Outlook form region, this is a good start, and I think it can be useful for any one that has as many appointments as I have. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)