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.
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:
[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.
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:
private void AppFormRegion_FormRegionShowing(object sender, System.EventArgs e)
{
try
{
Microsoft.Office.Interop.Outlook.Items oItems =
(Microsoft.Office.Interop.Outlook.Items)oCalendar.Items;
Microsoft.Office.Interop.Outlook.MeetingItem appM =
(Microsoft.Office.Interop.Outlook.MeetingItem)this.OutlookItem;
Microsoft.Office.Interop.Outlook.AppointmentItem appItem =
appM.GetAssociatedAppointment(false);
string startdate = appItem.Start.Date.ToShortDateString() + " 01:00";
string enddate = appItem.Start.Date.ToShortDateString() + " 23:00";
dayView1.StartDate = appItem.Start.Date;
dayView1.StartHour = appItem.Start.Hour-2;
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;
}
m_Appointments.Add(m_Appointment);
}
}
catch (Exception ex)
{
return;
}
}
All we got left to do is show our meeting on the calendar control:
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.