检测当前在Outlook中是否正在编辑电子邮件?

时间:2019-02-14 19:45:42

标签: vba outlook outlook-vba

我有一个在Application_NewMail事件上运行的宏-但是我已经看到,如果用户当前正在编写电子邮件或回复,它会产生怪异的影响-有时会使Outlook崩溃并失去进度。

有没有一种方法可以检测用户当前是否正在撰写电子邮件?

这将允许我取消宏并避免打扰用户。

1 个答案:

答案 0 :(得分:3)

我能够从相关问题中找到点点滴滴,但没有考虑到弹出电子邮件编辑器和内联响应。这是我整理的解决方案(似乎涵盖了所有基础):

Private Function IsUserEditing() As Boolean
    ' Check if the user is composing an email. Don't interrupt them if we are.
    ' 1. Check if the user has the pop-up email 'inspector' window open
    If Not (Application.ActiveInspector Is Nothing) Then
        Dim OpenWindow As Variant
        Set OpenWindow = Application.ActiveInspector.CurrentItem
        If TypeOf OpenWindow Is MailItem Then
            Dim NewMail As MailItem
            Set NewMail = OpenWindow
            ' Check if the mail they're viewing is not 'Sent' (i.e. being edited)
            If Not (NewMail.Sent) Then
                IsUserEditing = True
                Exit Function
            End If
        End If
    ' 2. Check if the user is replying to an email using the 'inline response' feature
    ElseIf Not (Application.ActiveExplorer.ActiveInlineResponse Is Nothing) Then
        IsUserEditing = True
        Exit Function
    End If
    IsUserEditing = False
End Function

可以这样使用:

Private Sub Application_NewMail()
    Debug.Print "New mail received..."        
    ' Check if the user is composing an email. Don't interrupt them if we are.
    If IsUserEditing Then
        Debug.Print "User appears to be composing an email. Cancelling..."
        Exit Sub
    End If        
    ' Otherwise Proceed
    PerformOnNewMailActions
End Sub

希望这对其他人有帮助!