首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用VBA更新Outlook应用程序?

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

Stack Overflow用户
提问于 2016-10-24 12:33:48
回答 2查看 2.6K关注 0票数 0

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

VBA子:

代码语言:javascript
复制
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

输出:

情人节 情人节 情人节

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-24 15:12:07

您正在修改和保存不同的对象--每次调用oItems.Item(i)时,您都会得到一个全新的COM对象,该对象不一定了解该对象的其他实例。有时Outlook缓存最后使用的对象,有时则不缓存。将该项存储在专用变量中。更普遍的是,多点符号(如oItem.Item(1))总是一个坏主意。

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2016-10-24 12:41:25

如果您将项设置为AppointmentItem.类型的变量,则似乎可以使用该项。

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40218574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档