我编写了一个Visual Basic宏,以再次将已打开的电子邮件标记为“未读”。为此,我使用了事件“ Application_ItemLoad”,如下所示。但是,在针对Outlook的分组对话函数的电子邮件对话进行测试时,我发现有些奇怪:
双击该对话组的最后一个元素,将不会触发该事件。
请启发我,为什么每隔一封电子邮件都会触发此事件,而不会出现任何问题,而仅针对对话组的最后一个元素触发该事件。
我可能找到了。在我的助手模块中,我有以下内容:Public EventsDisable, unReadWhenSelected As Boolean
。
[摘自Stack Overflow post和里克·罗斯斯坦对DailyDoseOfExcel.com的贡献,我了解到,用逗号分隔声明变量会导致第一个变量声明为Variant而不是预期的Boolean 。
我只是更改为以下内容,并且错误已消失。
Public EventsDisable As Boolean
Public unReadWhenSelected As Boolean
我仍然无法解释为什么将EventsDisable声明为Variant时会发生这种奇怪的错误。因此,如果有人知道,请放心。
Option Explicit
' When doubleclick opening unread mails, they are automatically marked 'read'. This code will mark them 'unread'.
' Despite that, emails that have been read already and then doubleclick opened will stay 'read'.
' In other words: Doubleclicking a mail won't change its unread property
Public WithEvents myItem As Outlook.mailItem
Private Sub Application_ItemLoad(ByVal Item As Object)
MsgBox "Item loaded"
'If EventsDisable = True Then Exit Sub
If Item.Class = olMail Then
Set myItem = Item
End If
End Sub
Private Sub myItem_Read()
unReadWhenSelected = myItem.UnRead
End Sub
Private Sub myItem_PropertyChange(ByVal Name As String)
If EventsDisable = True Then Exit Sub
If Name = "UnRead" Then
If unReadWhenSelected = True And myItem.UnRead = False Then
myItem.UnRead = True
myItem.Save
End If
End If
End Sub