共享日历中指定时间段内闲/忙日程安排
此代码示例获取日历中指定一周内的闲/忙日程安排,并向用户显示闲、忙和主题详细信息。
示例
该代码示例使用 Folder 对象的 GetCalendarExporter 方法为默认"日历"文件夹获取特定一周内的 CalendarSharing 对象。 然后,该示例对 CalendarSharing 对象调用 ForwardAsICal 方法并显示包含 iCalendar 有效负载的消息。
如果使用 Visual Studio 测试此代码示例,必须先添加对 Microsoft Outlook 15.0 对象库组件的引用,并在导入 Microsoft.Office.Interop.Outlook 命名空间时指定 Outlook 变量。 不得将 Imports 或 using 语句直接添加到此代码示例中的函数前面,这两个语句必须后跟公共类声明。 下面的代码行演示了如何在 Visual Basic 和 C# 中执行导入和分配。
Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub DemoCalendarSharing()
' Get instance of CalendarSharing object
Dim calShare As Outlook.CalendarSharing = _
Application.Session.GetDefaultFolder( _
Outlook.OlDefaultFolders.olFolderCalendar). _
GetCalendarExporter()
' Free busy and subject details
calShare.CalendarDetail = _
Outlook.OlCalendarDetail.olFreeBusyAndSubject
' Set start and end dates
calShare.StartDate = DateTime.Today
calShare.EndDate = calShare.StartDate.AddDays(1)
' Call ForwardAsICal method
Dim mail As Outlook.MailItem = _
calShare.ForwardAsICal( _
Outlook.OlCalendarMailFormat.olCalendarMailFormatDailySchedule)
' Add recipient
mail.Recipients.Add("someone@example.com")
mail.Recipients.ResolveAll()
' Set subject
Dim CalName As String = _
Application.Session.GetDefaultFolder( _
Outlook.OlDefaultFolders.olFolderCalendar).Name
mail.Subject = _
Application.Session.CurrentUser.Name & _
CalName.PadLeft(CalName.Length + 1)
' Display calendar sharing item
mail.Display(False)
End Sub
private void DemoCalendarSharing()
{
// Get instance of CalendarSharing object
Outlook.CalendarSharing calShare =
Application.Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderCalendar).
GetCalendarExporter();
// Free busy and subject details
calShare.CalendarDetail =
Outlook.OlCalendarDetail.olFreeBusyAndSubject;
// Set start and end dates
calShare.StartDate = DateTime.Today;
calShare.EndDate = calShare.StartDate.AddDays(1);
// Call ForwardAsICal method
Outlook.MailItem mail =
calShare.ForwardAsICal(Outlook.OlCalendarMailFormat
.olCalendarMailFormatDailySchedule);
// Add recipient
mail.Recipients.Add("someone@example.com");
mail.Recipients.ResolveAll();
// Set subject
string CalName =
Application.Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderCalendar).Name;
mail.Subject =
Application.Session.CurrentUser.Name +
CalName.PadLeft(CalName.Length + 1);
// Display calendar sharing item
mail.Display(false);
}