使用ItemChange事件处理程序更新项目属性 [英] Use ItemChange event handler to update an item property

查看:62
本文介绍了使用ItemChange事件处理程序更新项目属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过其他程序更新的Outlook中的联系人列表.我自己不碰这份清单.每次更改联系人时,我都想通过VBA更新公司名称.

I have a list of contacts in Outlook updated through other program. I don't touch the list myself. Every time a contact is changed, I want to update company name through VBA.

Public WithEvents objNewContact As Items   
Public Sub Initialize_handler()
    Set objNewContact = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
End Sub

Public Sub objNewContact_ItemChange(ByVal Item As Object)
    Item.CompanyName = "NewCompanyName"
    MsgBox "Company name changed to " & Item.CompanyName
End Sub

如果我通过Outlook自己编辑联系人,则可以使用.如果通过其他来源进行编辑,则会显示MsgBox,告诉我公司名称已更改,但更改未保存.

If I edit the contact myself through Outlook, it works. If edited through other source, it shows the MsgBox telling me that the company name has been changed, but the change is not saved.

如果我添加"Item.Save",则会创建一个无限循环.

If I add "Item.Save" it creates an infinite loop.

Public Sub objNewContact_ItemChange(ByVal Item As Object)
    Item.CompanyName = "NewCompanyName"
    MsgBox "Company name changed to " & Item.CompanyName
    Item.Save
End Sub

推荐答案

您可以在更改Item.CompanyName之前关闭ItemChange事件处理程序.

You could toggle the ItemChange event handler off before changing Item.CompanyName.

Public Sub objNewContact_ItemChange(ByVal Item As Object)

    ' event handler off
    Set objNewContact = Nothing

    Item.CompanyName = "NewCompanyName"
    Item.Save

    MsgBox "Company name changed to " & Item.CompanyName

    ' event handler on
    Initialize_handler

End Sub

这篇关于使用ItemChange事件处理程序更新项目属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆