如何使用Outlook VBA创建收件箱中未读邮件数量的自动回复?



我正在尝试触发带有规则的脚本来发送自动回复。

"您好,感谢您的邮件,您的邮件已经排好队列,您面前有XX封电子邮件,我们会尽快回复。">

XX 应该是未读电子邮件的数量。

我发现 Outlook 自动回复未读邮件计数:

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

它不起作用。

我在Outlook 2016上,有一个交换邮件服务器。

您的Items.ItemAdd 事件设置不正确,请尝试不使用 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

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

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

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

最新更新