【发布时间】:2020-02-04 11:40:42
【问题描述】:
outlook 中有几个邮件帐户。
有一个代码可以生成一个消息框,其中包含主邮箱中新邮件的属性。它适用于我的主要邮件帐户。
Option Explicit
Private WithEvents inboxItems As Outlook.Items
Private Sub Application_Startup()
Dim outlookApp As Outlook.Application
Dim objectNS As Outlook.NameSpace
Set outlookApp = Outlook.Application
Set objectNS = outlookApp.GetNamespace("MAPI")
Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub inboxItems_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim MessageInfo
Dim Result
If TypeName(Item) = "MailItem" Then
MessageInfo = "" & _
"Sender : " & Item.SenderEmailAddress & vbCrLf & _
"Sent : " & Item.SentOn & vbCrLf & _
"Received : " & Item.ReceivedTime & vbCrLf & _
"Subject : " & Item.Subject & vbCrLf & _
"Size : " & Item.Size & vbCrLf & _
"Message Body : " & vbCrLf & Item.Body
Result = MsgBox(MessageInfo, vbOKOnly, "New Message Received")
End If
ExitNewItem:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ExitNewItem
End Sub
还有另一个邮箱“规格估计 RU41”。我的任务是为该邮箱的新传入邮件获取相同的弹出消息。 我换了行
Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
与
Set inboxItems = objectNS.Folders("Specification Estimation RU41") _
.Folders("Inbox").Items
所以整个代码看起来像这样:
Option Explicit
Private WithEvents inboxItems As Outlook.Items
Private Sub Application_Startup()
Dim outlookApp As Outlook.Application
Dim objectNS As Outlook.NameSpace
Set outlookApp = Outlook.Application
Set objectNS = outlookApp.GetNamespace("MAPI")
Set inboxItems = objectNS.Folders("Specification Estimation RU41") _
.Folders("Inbox").Items
End Sub
Private Sub inboxItems_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim MessageInfo
Dim Result
If TypeName(Item) = "MailItem" Then
MessageInfo = "" & _
"Sender : " & Item.SenderEmailAddress & vbCrLf & _
"Sent : " & Item.SentOn & vbCrLf & _
"Received : " & Item.ReceivedTime & vbCrLf & _
"Subject : " & Item.Subject & vbCrLf & _
"Size : " & Item.Size & vbCrLf & _
"Message Body : " & vbCrLf & Item.Body
Result = MsgBox(MessageInfo, vbOKOnly, "New Message Received")
End If
ExitNewItem:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ExitNewItem
End Sub
但这不起作用。没有错误消息,但对新邮件没有反应。
我怎样才能让它工作?
【问题讨论】:
-
您对第二个文件夹有权限吗?您可以尝试使用 VBA 在该文件夹中创建一些内容以进行检查。
-
@DavidG 是的,我有权限。代码在 ThisOutlookSession 中。我怎样才能做到这一点?我的意思是用 VBA 在这个文件夹中创建一些东西。
-
单步执行代码时,您确定 inboxItems 变量不是 Nothing?
-
@DmitryStreblechenko 不,这不是什么。它显示所有收件箱项目。 screenshot .