[英]VBA Detect outlook incoming email from Excel
I have tried to use the code listed in the link to detect a new outlook email from an excel macro. 我试图使用链接中列出的代码来检测来自Excel宏的新Outlook电子邮件。 This code has not worked for me so far.
到目前为止,这段代码对我不起作用。 I am not sure as to why.
我不确定为什么。 I am also not quite sure what needs to go into a class module, regular module or how to call it in order for it to be monitoring.
我也不太确定需要什么来进入类模块,常规模块或如何调用它才能进行监视。 I dont want to add this to outlook as suggested in the article, because I can not physically added it to all individuals who need to use, when I can simply send an excel file and reference their outlook.
我不想按照文章中的建议将其添加到Outlook中,因为当我可以简单地发送一个excel文件并引用他们的Outlook时,我无法将其物理地添加到所有需要使用的个人中。 I am trying to understand how with events works when capturing the outlook event and any help would be greatly appreciated.
我试图了解在捕获Outlook事件时事件如何工作,任何帮助将不胜感激。 Thank you.
谢谢。
Sub WorkWithNewMail()
Dim objOutlook As Outlook.ApplicationDim objAllNewMail As Outlook.Items
Dim objMyEmail As Outlook.MailItem
Set objOutlook = New Outlook.Application
Set objAllNewMail = objOutlook.NewMail
For Each objMyEmail In objAllNewMail
'Do something with every email received
Next
End Sub
Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objNewMailItems As Outlook.Items
Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)
Dim objEmail As Outlook.MailItem
'Ensure we are only working with e-mail items
If Item.Class<> OlItemType.olMailItem Then Exit Sub
Debug.Print "Message subject: " & objEmail.Subject
Debug.Print "Message sender: " & objEmail.SenderName &" (" &objEmail.SenderEmailAddress & ")";
Set objEmail = Nothing
End Sub
You misunderstood the article. 您误解了这篇文章。 The key point was "Unfortunately, there is no magical NewMail collection".
关键点是“不幸的是,没有神奇的NewMail集合”。
The working code is in the latter part of the article. 工作代码在本文的后半部分。 It is for Outlook not Excel, yet you may be able to get what you want.
它是针对Outlook而不是Excel,但您仍然可以得到想要的东西。
Try this first on your own Inbox, to see it working when a mailitem is added. 首先在您自己的收件箱中尝试此操作,以查看添加邮件项时它是否正常工作。
Note untested code. 注意未经测试的代码。 I may test later.
我可能稍后再测试。
In the ThisOutlookSession module 在ThisOutlookSession模块中
Option Explicit
Private WithEvents objNewMailItems As Items
Private Sub Application_Startup()
dim objNS as namespace
Dim objMyInbox As Folder
Set objNS = GetNamespace("MAPI")
' This references your inbox.
Set objMyInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objNewMailItems = objMyInbox.Items
Set objNS = Nothing
Set objMyInbox = Nothing
End Sub
Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)
'Ensure we are only working with e-mail items
If Item.Class<> olMail Then Exit Sub
Debug.Print "Message subject: " & Item.Subject
Debug.Print "Message sender: " & Item.SenderName & _
" (" & Item.SenderEmailAddress & ")"
End Sub
Re: "when I can simply send an excel file and reference their outlook." 回复:“当我可以简单地发送一个excel文件并引用其前景时。” If you have been given permission, you reference someone else's inbox as described here.
如果您被授予权限,则可以参考此处所述引用其他人的收件箱。
Use a shared folder (Exchange mailbox) 使用共享文件夹(Exchange邮箱)
dim objNS as namespace
Dim objOwner As Recipient
Set objNS = GetNamespace("MAPI")
Set objOwner = objNS.CreateRecipient("name , alias or email address")
objOwner.Resolve
If objOwner.Resolved Then
'MsgBox objOwner.Name
Set objOwnerInbox = objNS.GetSharedDefaultFolder(objOwner, olFolderInbox)
End If
Putting this all together 全部放在一起
Once again in your own ThisOutlookSession module 再次在您自己的ThisOutlookSession模块中
Replace the original Application_Startup code 替换原始的Application_Startup代码
Option Explicit
Private WithEvents objOwnerInboxItems As Outlook.Items
Private Sub Application_Startup()
dim objNS as namespace
Dim objOwner As Recipient
Dim objOwnerInbox As Folder
Set objNS = GetNamespace("MAPI")
' As described in the article
' You can use the mailbox owner's display name, alias, or email address when resolving the recipient.
Set objOwner = objNS.CreateRecipient("name , alias or email address")
objOwner.Resolve
If objOwner.Resolved Then
'MsgBox objOwner.Name
' If the owner has given you permission
Set objOwnerInbox = objNS.GetSharedDefaultFolder(objOwner, olFolderInbox)
Set objOwnerInboxItems = objOwnerInbox.Items
End if
Set objNS = Nothing
Set objOwner = Nothing
Set objOwnerInbox = Nothing
End Sub
Private Sub objOwnerInboxItems_ItemAdd(ByVal Item As Object)
'Ensure we are only working with e-mail items
If Item.Class<> olMail Then Exit Sub
Debug.Print "Message subject: " & Item.Subject
Debug.Print "Message sender: " & Item.SenderName & _
" (" & item.SenderEmailAddress & ")"
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.