如何使用 VBA 更新 Outlook 应用程序?

How to update an Outlook appoiment using VBA?

如何使用 VBA update/edit 现有的日历约会?为什么以下 VBA 代码无法更新主题?

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

输出:

Valentine's Day

Valentine's Day

Valentine's Day

如果将项目设置为 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

输出:

Valentine's Day

foo

foo

您正在修改和保存不同的对象 - 每次调用 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