简体   繁体   English

VBA Outlook 2010:监视公共文件夹中的新电子邮件

[英]VBA Outlook 2010: Monitor new emails in public fodler

whenever a new mail arrives in a public folder, I would like a MsgBox to pop up. 每当有新邮件到达公用文件夹时,我都希望弹出一个MsgBox。 I solved this for my own inbox using this code: 我使用以下代码为自己的收件箱解决了此问题:

Private Sub Application_NewMail()

Dim oNS         As NameSpace
Dim oFolder     As MAPIFolder
Dim oNewMail    As MailItem

Set oNS = Application.GetNamespace("MAPI")
Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
Set oNewMail = oFolder.Items.GetFirst

MsgBox oNewMail.subject    

End Sub

I also managed to access and retrieve the latest email from the public folder by replacing: 我还设法通过替换以下内容来访问和检索公用文件夹中的最新电子邮件:

Set oFolder = oNS.GetDefaultFolder(olFolderInbox)

by 通过

Set oFolder = oNS.Folders(2).Folders(2).Folders("XX").Folders("XX")

Howver, this obviously only works, when I manually evalute the code since the code is only executed when a new mail arrives in my inbox. 但是,这显然仅在我手动评估代码时有效,因为仅当新邮件到达收件箱时才执行代码。 I did some googling and found a potential solution to monitor a public folder: 我进行了一些谷歌搜索,发现了一个监视公用文件夹的潜在解决方案:

Private WithEvents TestMail As Items

Public Sub Application_Startup()
    Set TestMail = Application.GetNamespace("MAPI").Folders(2).Folders(2).Folders("XX").Folders("XX").Items
End Sub

Private Sub TestMail_ItemAdd(ByVal Item As Object)
    MsgBox ("new mails arrived")
End Sub

Edit - The error when compiling: Unknown attribute in sub or function. 编辑-编译时出现错误:子或函数中的未知属性。 I am using Outlook 2010 professional. 我正在使用Outlook 2010 Professional。

Try to use the following code: 尝试使用以下代码:

Private WithEvents NewMail As Items

Public Sub Application_Startup()
   Set NewMail = Application.GetNamespace("MAPI").Folders(2).Folders(2).Folders("XX").Folders("XX").Items
End Sub

Private Sub NewMail_ItemAdd(ByVal Item As Object)
   MsgBox ("new mails arrived")
End Sub

However, I'd recommend breaking the long chain of calls: 但是,我建议您断开长途电话:

Set NewMail = Application.GetNamespace("MAPI").Folders(2).Folders(2).Folders("XX").Folders("XX").Items

and declare each property or method call on a separate line of code. 并在单独的代码行上声明每个属性或方法调用。 Thus, you will find the exact ptoperty or method call which generates the error. 因此,您将找到产生错误的确切ptoperty或方法调用。

You may find the How to get reference to Public Folder Store using Outlook Object Model for Outlook 2010? 您可能会发现如何使用Outlook 2010的Outlook对象模型获取对公用文件夹存储的引用? article helpful. 文章很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM