打开电子邮件时运行宏的规则

时间:2014-02-12 12:19:36

标签: vba outlook outlook-vba

我为已打开的邮件创建了一个宏,它可以执行某些操作。我想创建一个规则,当我打开邮件时,它会自动执行。

我不希望这条规则一直运行,就在我打开邮件时,我不想在我收到的每封邮件上强制执行此规则。

1 个答案:

答案 0 :(得分:5)

关注@ZZA评论,

试试这段代码:

Public WithEvents myItem As Outlook.MailItem

Private Sub Application_ItemLoad(ByVal Item As Object)
    If Item.Class = olMail Then
        Set myItem = Item
    End If
End Sub


Private Sub myItem_Open(Cancel As Boolean)

   'Your code
End Sub

在ThisOutlookSession中粘贴代码 enter image description here

修改

为避免'Your code触发事件,我们需要一个事件禁用器:

Public WithEvents myItem As Outlook.MailItem
Public EventsDisable as Boolean

Private Sub Application_ItemLoad(ByVal Item As Object)
    If EventsDisable = True Then Exit Sub
    If Item.Class = olMail Then
        Set myItem = Item
    End If
End Sub


Private Sub myItem_Open(Cancel As Boolean)
    EventsDisable=True
   'Your code
    EventsDisable=False
End Sub