共用方式為


建立從太平洋時區開始並結束於東部時區的約會

有時候,約會可能會跨越一段時間,在此期間,使用者可能會前往與約會開始時不同的時區。 本範例會建立一個約會,該約會會從太平洋時區開始 (UTC-8) ,並在東部時區 (UTC-5) 結束。

範例

此程式代碼範例會使用 TimeZones 物件,代表 Microsoft Windows 中辨識的所有時區。 它也會使用 TimeZone 物件來設定或取得 AppointmentItem 物件上的 StartTimeZone 屬性和 EndTimeZone 屬性。

Outlook 會以當地時間顯示所有日期,以使用者目前的時區表示,由 Windows 控制台 中的使用者設定所控制。 Outlook 也會在當地時間中設定或取得屬性,例如 StartEnd。 不過,Outlook 會將日期和時間值儲存為國際標準時間 (UTC) ,而不是本地時間。 如果您使用 PropertyAccessor 物件檢查 Appointment.Start 的內部值,您會發現內部日期和時間值等於轉換成對等 UTC 日期和時間值的本機日期和時間值。

Outlook 會使用時區資訊,將約會對應至儲存約會時的正確 UTC 時間,以及在行事曆中顯示專案時的正確本地時間。 變更 StartTimeZone 會影響 Appointment.Start 的值,該值一律以當地時區表示,由 TimeZones 所傳回物件的 CurrentTimeZone 屬性表示。 同樣地,變更 EndTimeZone 會影響 Appointment.End 的值,該值一律以本機時區表示,以 Application.TimeZones 所傳回之物件的 CurrentTimeZone 属性表示。

您可以使用 Windows 登錄中 TimeZone 的地區設定獨立密鑰,從 TimeZones 物件擷取特定的 TimeZone。 與地區設定無關的 TimeZone 金鑰列在下列索引鍵之下: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\TimeZones

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 TimeZoneExample()
    Dim appt As Outlook.AppointmentItem = _
        CType(Application.CreateItem( _
        Outlook.OlItemType.olAppointmentItem), Outlook.AppointmentItem)
    Dim tzs As Outlook.TimeZones = Application.TimeZones
    ' Obtain timezone using indexer and locale-independent key
    Dim tzEastern As Outlook.TimeZone = tzs("Eastern Standard Time")
    Dim tzPacific As Outlook.TimeZone = tzs("Pacific Standard Time")
    appt.Subject = "SEA - JFK Flight"
    appt.Start = DateTime.Parse("8/9/2006 8:00 AM")
    appt.StartTimeZone = tzPacific
    appt.End = DateTime.Parse("8/9/2006 5:30 PM")
    appt.EndTimeZone = tzEastern
    appt.Display(False)
End Sub
private void TimeZoneExample()
{
    Outlook.AppointmentItem appt = Application.CreateItem(
        Outlook.OlItemType.olAppointmentItem)
        as Outlook.AppointmentItem;
    Outlook.TimeZones tzs = Application.TimeZones;
    // Obtain timezone using indexer and locale-independent key
    Outlook.TimeZone tzEastern = tzs["Eastern Standard Time"];
    Outlook.TimeZone tzPacific = tzs["Pacific Standard Time"];
    appt.Subject = "SEA - JFK Flight";
    appt.Start = DateTime.Parse("8/9/2006 8:00 AM");
    appt.StartTimeZone = tzPacific;
    appt.End = DateTime.Parse("8/9/2006 5:30 PM");
    appt.EndTimeZone = tzEastern; 
    appt.Display(false);
}

另請參閱