次の方法で共有


予定表をディスクに保存する

この例では、予定表全体を iCalendar (.ics) ファイル形式でディスクに保存する方法を示します。

このコード サンプルでは、予定表全体または予定表のある範囲の予定をディスクに保存する処理をサポートする CalendarSharing オブジェクトを使用します。 Outlook では、定期的な予定が .ics ファイルに個別の予定として保存されないように, .ics ファイルが自動的に最適化されます。 保存する予定表のサイズによっては、ディスクへの予定表の保存にはかなりの時間がかかる場合があります。 予定表を保存している間、ユーザーは Outlook ウィンドウを操作できません。

コード サンプルでは、最初に、既定の予定表フォルダーで GetCalendarExporter を呼び出して、 CalendarSharing オブジェクトを取得します。 次に、 CalendarSharing オブジェクトのプロパティを設定し、予定表全体を保存するかどうかや、"非公開" に指定されている予定の詳細を含めるかどうかといった、エクスポートの条件を指定します。

予定表の情報を .ics ファイル形式に保存するには、CalendarSharing オブジェクトの SaveAsICal メソッドを呼び出します。

Visual Studio を使用してこのコード例をテストする場合、Microsoft.Office.Interop.Outlook 名前空間をインポートするときに、まず Microsoft Outlook 15.0 オブジェクト ライブラリ コンポーネントへの参照を追加し、Outlook 変数を指定します。 Imports または using ステートメントは、コード例の関数の前に直接置くことはできません。パブリック Class 宣言の前に追加する必要があります。 次のコード行は、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);
}

関連項目