【问题标题】:Last element of an Outlook email conversation doesn't trigger the Application_ItemLoad event in VBAOutlook 电子邮件对话的最后一个元素不会触发 VBA 中的 Application_ItemLoad 事件
【发布时间】:2020-03-17 15:52:43
【问题描述】:

我编写了一个 Visual Basic 宏来再次将打开的电子邮件标记为“未读”。 为此,我使用了事件“Application_ItemLoad”,如下所示。然而,在测试 Outlook 的 按对话分组 功能的电子邮件对话时,我注意到了一些奇怪的事情:

通过双击打开此类对话组的最后一个元素时,该事件根本不会触发。

请告诉我为什么对于其他每封电子邮件,此事件都会触发而没有任何问题,但仅针对对话组的最后一个元素不会触发。

【问题讨论】:

    标签: vba events outlook


    【解决方案1】:

    我可能已经找到了。 在我的帮助模块中,我有这个:
    Public EventsDisable, unReadWhenSelected As Boolean

    从伟大的 Stack Overflow post 和 Rick Rothstein 对 DailyDoseOfExcel.com 的贡献中,我了解到用逗号分隔声明变量会导致第一个变量被声明为 Variant 而不是预期的布尔值

    我刚改成下面的,错误消失了

    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
    

    【讨论】:

      猜你喜欢
      • 2011-12-22
      • 2022-11-11
      • 2021-07-29
      • 2021-06-16
      • 2022-01-19
      • 2021-11-03
      • 2017-03-06
      • 2014-09-05
      • 1970-01-01
      相关资源
      最近更新 更多