65.9K
CodeProject 正在变化。 阅读更多。
Home

Outlook 2007 日历预览

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2012 年 3 月 20 日

CPOL

1分钟阅读

viewsIcon

20925

downloadIcon

800

一个具有 Outlook 2007 日历预览的窗体区域。

介绍 

Outlook 2010 中最酷的功能之一是日历预览。对于我们这些仍然使用 Outlook 2007 的人来说,这就是日历预览。

背景

我们公司近期没有升级到 Outlook 2010 的计划。但我仍然希望在我的 Outlook 中拥有这个功能。我有很多约会,需要立即查看冲突。切换到日历查看冲突,然后再返回会议请求,浪费了很多时间。所以我决定编写自己的日历预览控件,并将其放置在会议请求上。为此,我使用了从 这里 获得的窗体区域控件和日历控件。感谢 Ertan Tike

使用代码

好的,正如我所说,我正在使用 Outlook 2007 的 VSTO 项目和一个相邻的窗体区域。代码非常简单,再次感谢 Ertan Tike 提供的优秀控件,它完成了显示类似日历控件的所有工作。

让我们从窗体区域控件的窗体区域工厂部分开始。我们需要声明我们想要使用的消息类型。因为我们处理的是会议,我们需要使用这个消息类型:

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

接下来,让我们看看 FormRegionInitializing 事件。在这里,我们连接到 Outlook 应用程序和日历文件夹。

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);
}

下一步是 FormRegionShowing 事件

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;
    }
}

我们只需要在日历控件上显示我们的会议即可

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;
    }
}

就这样了。

兴趣点 

如果您不熟悉 Outlook 窗体区域,这是一个好的起点,我认为对于像我一样有很多约会的人来说,它会很有用。

Outlook 2007 日历预览 - CodeProject - 代码之家
© . All rights reserved.