检测上次选择的电子邮件是否已标记为已读并提示保存

时间:2016-01-16 19:36:59

标签: events outlook outlook-vba

我的观点VBA代码旨在执行以下操作: 如果选择更改(即用户点击收件箱中的其他电子邮件)

If [the previously selected email was originally 'Unread' and just became 'Read'] Then
Prompt the user to save the previous email
Else
Do Nothing
End If

为此,我使用了Explorer_SelectionChange事件。我面临的问题是,在将上一封电子邮件标记为已读之前,Outlook需要大约1到2秒!我的代码在这2秒钟之​​前执行。因此,它始终将之前的电子邮件视为未读! :(

我试图向我的Sub引入暂停,但它没有用。 Outlook等待我的代码完成,包括暂停,然后依次等待1到2秒,然后将上一封电子邮件标记为已读。

总而言之,我的问题是:是否有一个事件可以识别以前选择的电子邮件何时标记为已读? (PS:我尝试了MailItem.Read事件,但它也是即时的,适用于所有“已阅读和未阅读”的电子邮件]

以下是我的代码中专门尝试实现上述功能的部分:

Public WithEvents myOlExp As Outlook.Explorer
Dim Flag As Integer
Dim oMail As Outlook.MailItem

Private Sub Application_Startup()
Dim objItem As Object

Set myOlExp = Application.ActiveExplorer
enviro = CStr(Environ("USERPROFILE"))

'Identify the status of the selected email at startup
For Each objItem In myOlExp.Selection
    If objItem.MessageClass = "IPM.Note" Then
        Set oMail = objItem
    End If
Next

If oMail.UnRead Then
    Flag = 1 'Means Current selection is an unread email
Else
    Flag = 0 'Means Current selection has been read before
End If

End Sub


Private Sub myOlExp_SelectionChange()
'If previous selected email was Unread
If Flag = 1 Then
    If oMail.UnRead = False Then
        MsgBox "previous email has just been read do you want to save?"
        '^^This is where the problem happens: the previously selected email is always seen as read by the code
        'because Outlook takes 1-2 seconds after the selection change event before it marks the email as read!!
    Else
        MsgBox "Previous email still marked as unread, do nothing"
        '^^I am always getting this outcom when I change selection from an unread email to another email!
    End If

    'Now identify the status of the newly selected email
    For Each objItem In myOlExp.Selection
        If objItem.MessageClass = "IPM.Note" Then
            Set oMail = objItem
        End If
    Next

    If oMail.UnRead Then
        Flag = 1 'Means Current selection is an unread email
    Else
        Flag = 0 'Means Current selection has been read before
    End If

Else
    ' Flag = 0 i.e previous email was already read
    ' Identify the status of the newly selected item.
    For Each objItem In myOlExp.Selection
        If objItem.MessageClass = "IPM.Note" Then
            Set oMail = objItem
        End If
    Next
    If oMail.UnRead Then
        Flag = 1
    Else
        Flag = 0
    End If
End If

End Sub

我希望我能清楚地表达我的问题!非常感谢任何帮助。

非常感谢

1 个答案:

答案 0 :(得分:0)

设置Flag = 1后,oMail.UnRead状态无关紧要。

If Flag = 1 Then

    ' Remove this test
    'If oMail.UnRead = False Then

        MsgBox "...