共用方式為


將行事曆儲存至磁碟

此範例示範如何以 iCalendar 檔案格式將整個行事曆儲存至磁碟 (.ics) 。

範例

此程式代碼範例使用 CalendarSharing 物件,此物件支援將整份行事曆或一系列約會從行事曆儲存到磁碟。 Outlook 會自動優化.ics檔案,使週期性約會不會儲存為.ics檔案中的個別約會。 根據要儲存的行事曆大小,將行事曆儲存到磁碟可能需要相當長的時間。 儲存行事歷時,Outlook 視窗對使用者似乎沒有回應。

程序代碼範例會先呼叫預設 Calendar 資料夾上的 GetCalendarExporter ,以取得 CalendarSharing 物件。 然後它會設定 CalendarSharing 物件的屬性來指定導出的準則,例如是否要儲存整個行事歷,並包含標示為「私人」之約會的詳細數據。

若要以.ics檔格式儲存行事曆資訊,程式代碼範例會呼叫 CalendarSharing 物件的 SaveAsICal 方法。

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and 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);
}

另請參閱