Outlook VBA用于在收件箱中创建包含未读邮件数量的自动回复

时间:2017-05-09 10:53:35

标签: vba email outlook outlook-vba outlook-2016

我正在尝试创建一个自动回复,我可以在其中触发带有规则的脚本来发送自动回复。

"您好,感谢您的邮件,您的邮件已排入队列,您面前有 XX 的电子邮件数量,我们会尽快回复。 #34; XX应该是未读电子邮件的数量。

我发现了这个:

Private Sub myOlItems_ItemAdd(ByVal Item As Object)

End Sub

Sub AutoResponse(objmsg As Outlook.MailItem)

    ' define my reply message
    Dim objReply As MailItem
    ' let's get ourselves the inbox!
    Dim inbox As MAPIFolder
    Set inbox = Application.GetNamespace("MAPI"). _
    GetDefaultFolder(olFolderInbox)

    ' Let's get this reply going!
    Set objReply = objmsg.Reply
    ' Subject Re: their subject. Standard
    objReply.Subject = "Re: " & objReply.Subject
    ' Body - you define this, use the variable for the unread count in inbox
    objReply.Body = "Your email has been received. I currently have " & inbox.UnReadItemCount & " unread emails in my inbox and I will get yours as soon as I can"

    ' Send this thing!
    objReply.Send
    ' Reset
    Set objReply = Nothing

End Sub

Here

但它似乎并没有起作用。 我在Outlook 2016上,使用Exchange邮件服务器。

由于

2 个答案:

答案 0 :(得分:1)

您需要手动在Outlook中创建规则,并将VBA宏子(AutoResponse)分配给规则。只有这样你才能运行代码。

作为一种可能的解决方法,您可以处理在收件箱中收到新项目时触发的应用程序的NewMailEx事件。当新邮件到达收件箱时以及客户端规则处理发生之前,事件将触发。您可以使用EntryIDCollection数组中返回的条目ID来调用NameSpace.GetItemFromID方法并处理该项。有关详细信息,请参阅Outlook's Rules and Alerts: Run a Script

对于具有Exchange Server帐户(非缓存Exchange模式或缓存Exchange模式)的用户,只有在Outlook启动后到达服务器的邮件才会触发该事件。在Outlook启动后立即在缓存Exchange模式下同步的邮件不会触发事件,也不会触发Outlook在非缓存Exchange模式下启动时服务器上已有的邮件。

答案 1 :(得分:1)

您的 Items.ItemAdd Event 设置不正确,请尝试不使用Outlook规则的流动代码,确保重新启动Outlook

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
    Dim olNs As Outlook.NameSpace
    Dim Inbox  As Outlook.MAPIFolder

    Set olNs = Application.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
    Set Items = Inbox.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)

    If TypeOf Item Is Outlook.mailitem Then
        AutoResponse Items
    End If

End Sub

Private Sub AutoResponse(Item As Outlook.mailitem)
    Dim Reply As Outlook.mailitem ' Reply msg
    Dim Inbox As Outlook.MAPIFolder ' Inbox

    Set Inbox = Application.GetNamespace("MAPI") _
                .GetDefaultFolder(olFolderInbox)

    ' Let's get this reply going!
    Set Reply = Item.Reply
    ' Subject Re: their subject. Standard
    Reply.subject = Reply.subject

    ' Body - you define this, use the variable for the unread count in inbox
    Reply.HTMLBody = "Your email has been received. I currently have " & _
                      Inbox.UnReadItemCount & _
              " unread emails in my inbox and I will get yours as soon as I can"

    ' Send this thing!
    Reply.Send
    ' Reset
    Set Reply = Nothing

End Sub