将日历保存到磁盘中

此代码示例展示了如何以 iCalendar (.ics) 文件格式将整个日历保存到磁盘中。

示例

该代码示例使用 CalendarSharing 对象,该对象支持将整个日历或日历中的一组约会保存到磁盘上。 Outlook 会自动优化 .ics 文件,以便定期约会在 .ics 文件中不会另存为单个约会。 根据要保存的日历的大小,将日历保存到磁盘上可能需要很长时间。 在保存日历时,Outlook 窗口似乎对用户为无响应。

该代码示例先对默认"日历"文件夹调用 GetCalendarExporter ,以获取 CalendarSharing 对象。 然后它设置 CalendarSharing 对象的属性来指定导出条件,例如是否保存整个日历并包括标记为"个人"的约会的详细信息。

此代码示例调用 CalendarSharing 对象的 SaveAsICal 方法,以 .ics 文件格式保存日历。

如果使用 Visual Studio 测试此代码示例,必须先添加对 Microsoft Outlook 15.0 对象库组件的引用,并在导入 Microsoft.Office.Interop.Outlook 命名空间时指定 Outlook 变量。 不得将 Importsusing 语句直接添加到此代码示例中的函数前面,这两个语句必须后跟公共类声明。 下面的代码行演示了如何在 Visual Basic 和 C# 中执行导入和分配。

Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub SaveCalendarToDisk(ByVal calendarFileName As String)
    If String.IsNullOrEmpty(calendarFileName) Then
        Throw New ArgumentException( _
        "Parameter must contain a value.", "calendarFileName")
    End If

    Dim calendar As Outlook.Folder = TryCast( _
        Application.Session.GetDefaultFolder(_
        Outlook.OlDefaultFolders.olFolderCalendar), Outlook.Folder)
    Dim exporter As Outlook.CalendarSharing = _
        calendar.GetCalendarExporter()

    '' Set the properties for the export
    exporter.CalendarDetail = Outlook.OlCalendarDetail.olFullDetails
    exporter.IncludeAttachments = True
    exporter.IncludePrivateDetails = True
    exporter.RestrictToWorkingHours = False
    exporter.IncludeWholeCalendar = True

    '' Save the calendar to disk
    exporter.SaveAsICal(calendarFileName)
End Sub
private void SaveCalendarToDisk(string calendarFileName)
{
    if (string.IsNullOrEmpty(calendarFileName))
        throw new ArgumentException("calendarFileName", 
        "Parameter must contain a value.");

    Outlook.Folder calendar = Application.Session.GetDefaultFolder(
        Outlook.OlDefaultFolders.olFolderCalendar) as Outlook.Folder;
    Outlook.CalendarSharing exporter = calendar.GetCalendarExporter();

    // Set the properties for the export
    exporter.CalendarDetail = Outlook.OlCalendarDetail.olFullDetails;
    exporter.IncludeAttachments = true;
    exporter.IncludePrivateDetails = true;
    exporter.RestrictToWorkingHours = false;
    exporter.IncludeWholeCalendar = true;

    // Save the calendar to disk
    exporter.SaveAsICal(calendarFileName);
}

另请参阅