如何使用VBA更新/编辑现有的日历约会?为什么下面的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输出:
情人节 情人节 情人节
发布于 2016-10-24 15:12:07
您正在修改和保存不同的对象--每次调用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发布于 2016-10-24 12:41:25
如果您将项设置为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
https://stackoverflow.com/questions/40218574
复制相似问题