我基于教程here和here(ItemAdd方法)成功实现了我的第一个“对xyz文件夹中的每封新电子邮件执行操作”。是的!
我的下一个挑战是当我与Outlook客户不在线时运行此宏。对于无法访问Outlook服务器的典型公司情况,是否有解决方案?
我的后备解决方案不是在一夜之间关闭我的笔记本电脑...哈哈@sustainabilty。 谢谢!
我的宏看起来像
Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objNewMailItems As Outlook.Items
Private Sub Application_Startup()
Dim objMyInbox As Outlook.MAPIFolder
Set objNS = Application.GetNamespace("MAPI")
Set objMyInbox = objNS.Folders("inbox@host.com").Folders("Inbox")
Set objNewMailItems = objMyInbox.Items
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
'do something here with the incoming email
End Sub
答案 0 :(得分:1)
我迟到了7个月,我想现在您的问题可能已经解决了。 这是为像我这样的更多人而努力解决这个问题的。然而,它有一个简单的解决方案。 我已经面对这种问题几天了。我唯一想做的事情不是实际上使宏在脱机模式下工作,而是在应用程序启动时处理未读电子邮件并将其标记为已读。 我知道这并非您要找的解决方案,但值得一试。 帮助您不必每天晚上都打开笔记本电脑:P
您甚至不需要更改引用中的任何内容。
Public WithEvents InboxItems As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Object
Set olApp = CreateObject("Outlook.Application")
Dim olNS As Object
Set olNS = olApp.GetNamespace("MAPI")
Dim Inbox As Object
Set Inbox = olNS.Folders("dummy.dummy@dummy.com").Folders("Inbox") 'can also change it to any custom folder you want
Dim olMail As Object
Set olMail = olApp.CreateItem(olMailItem)
Set InboxItems = Inbox.Items
Dim Item As Object
'Process Items that were not processed because you were offline
For Each Item In InboxItems.Restrict("[UnRead] = True")
On Error GoTo ErrorHandler
Debug.Print Item.Subject 'Do anything you want
Item.UnRead = False
Next
Exit Sub
ErrorHandler:
Resume Next
Exit Sub
End Sub
Private Sub InboxItems_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler
Debug.Print Item.Subject 'Do anything you want
Item.UnRead = False
Exit Sub
ErrorHandler:
MsgBox "Error!"
Item.UnRead = False
Exit Sub
End Sub