2016-10-24 3 views
0

VBAを使用して既存の予定表の予定を更新または編集するにはどうすればよいですか?なぜ、次のVBAコードでは件名を更新できないのですか?VBAを使用してOutlook appoimentを更新するにはどうすればよいですか?

VBAサブ:

Sub failToEditAppointment() 
    Dim oSession As Variant 
    Set oSession = Application.Session 
    Dim oCalendar As Variant 
    Set oCalendar = oSession.GetDefaultFolder(olFolderCalendar) 
    Dim oItems As Variant 
    Set oItems = oCalendar.Items 

    oItems.IncludeRecurrences = False 
    oItems.Sort "[Location]" 

    Debug.Print oItems(1).Subject 
    oItems(1).Subject = "foo" 
    Debug.Print oItems(1).Subject 
    oItems(1).Save 
    Debug.Print oItems(1).Subject 
End Sub 

出力:

バレンタインデー

バレンタインデー

バレンタインデー

答えて

1

あなたは別のオブジェクトを変更して保存している - たびにoItems.Item(i)を呼び出すと、そのオブジェクトの他のインスタンスについて何も知っていることが保証されていない新しいCOMオブジェクトが返されます。場合によっては、Outlookが最後に使用したオブジェクトをキャッシュすることがあります。項目を専用の変数に格納します。より一般的な注意点として、複数のドット記法(oItem.Item(1)のように)は常に悪い考えです。

Dim oItem As Object 

oItems.IncludeRecurrences = False 
oItems.Sort "[Location]" 
set oItem = oItems(1) 
Debug.Print oItem.Subject 
oItem.Subject = "foo" 
Debug.Print oItem.Subject 
oItem.Save 
Debug.Print oItem.Subject 
0

変数をAppointmentItem型の変数に設定すると動作するようです。

Sub failToEditAppointment() 
    Dim oSession As Variant 
    Set oSession = Application.Session 
    Dim oCalendar As Variant 
    Set oCalendar = oSession.GetDefaultFolder(olFolderCalendar) 
    Dim oItems As Variant 
    Set oItems = oCalendar.Items 

    oItems.IncludeRecurrences = False 
    oItems.Sort "[Location]" 

    Dim appointment As AppointmentItem 
    Set appointment = oItems(1) 
    Debug.Print appointment.Subject 
    appointment.Subject = "foo" 
    Debug.Print appointment.Subject 
    appointment.Save 
    Debug.Print appointment.Subject 
End Sub 

出力:

バレンタインデー

fooの

fooの

関連する問題