VBA Outlook:脱机时在传入电子邮件中运行宏

问题描述 投票:0回答:1

根据教程herehere(ItemAdd方法),我实现了我的第一个“对xyz文件夹中的每封新电子邮件执行一些操作”。是的!

我的下一个挑战是当我不与Outlook客户端在线时运行此宏。对于无法访问Outlook服务器的典型公司情况,是否有解决方案?

我的后备解决方案不会在一夜之间关闭我的笔记本电脑...大声笑@sustainabilty。谢谢!

btw我的宏看起来像

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("[email protected]").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
vba outlook outlook-vba
1个回答
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("[email protected]").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


© www.soinside.com 2019 - 2024. All rights reserved.