【发布时间】:2016-04-26 19:51:30
【问题描述】:
在 Outlook 2010 中,使用下面的代码,我删除或移入垃圾文件夹的任何内容都会自动标记为已读。
Option Explicit
Dim WithEvents DeletedItems As Outlook.Items
Private Sub Application_Startup()
Set DeletedItems = Session.GetDefaultFolder(olFolderDeletedItems).Items
End Sub
Private Sub DeletedItems_ItemAdd(ByVal Item As Object)
If Item.UnRead = True Then
Item.UnRead = False
Item.Save
End If
End Sub
它在 Outlook 2013 中根本不起作用。
这是我用来检查 Outlook 如何查看已删除电子邮件的已读/未读状态的代码。我从here 中提升了Pause 函数。
Private Sub DeletedItems_ItemAdd(ByVal Item As Object)
RememberItem Item 'Remember which email this is
Debug.Print "At start: " & Item.UnRead 'Should be True
If Item.UnRead = True Then
Item.UnRead = False
Item.Save
End If
Debug.Print "After mark read: " & Item.UnRead 'Should be False
Pause 10 'In separate module. Code from https://stackoverflow.com/a/30196332/2623367
Debug.Print "After pause: " & Item.UnRead 'Should be False unless item has become Unread
End Sub
Private Function RememberItem(Optional ByVal Item As Object) As Object
'Allows check-up on the deleted item after event-handler is done with it.
Static oDeleted As Object
If Not Item Is Nothing Then Set oDeleted = Item
Set RememberItem = oDeleted
End Function
Private Sub CheckStatus()
Dim CheckItem As Object
Set CheckItem = RememberItem
Debug.Print "Follow-up check: " & CheckItem.UnRead 'Should be False
End Sub
我得到的输出:
- 开始时:真(项目未读 - 这是正确的)
- 标记读取后:错误(项目已读取 - 这可能正确也可能不正确)
- 暂停后:错误(已读取项目 - 这是不正确的)
- 后续检查:错误(项目已读取 - 这是不正确的)
更新:
标记为有效的答案确实解决了我的问题,尽管我偶尔仍会看到一些奇怪的行为。
进一步挖掘发现根本原因是 Outlook 和电子邮件服务器之间的同步问题。 Outlook 会删除一些东西,但同步会很麻烦,看起来 Outlook 在发回自己的更新之前从服务器中提取更新。这些差异似乎导致 Outlook 无法跟踪已删除电子邮件应处于的状态。
我的工作场所使用 Google Apps 作为他们的电子邮件提供商,并且我在 Outlook 中设置了正确的 IMAP 设置,但 Google 和 Outlook 无法正常运行。通过使用选定的答案和 Google 的 Outlook syncing tool for Google Apps,能够消除所有不可预测的行为。
还确认了我的原始代码与 Google Apps 同步工具结合使用时的行为。
我应该早点意识到问题可能是 Google 和 Outlook 一起出现了错误,但我什至没有想到,这就是为什么我之前没有提到这个等式中的 Google 组件。
【问题讨论】:
-
是否有任何错误信息?将项目放入已删除项目文件夹时会发生什么?代码运行了吗?
-
我在 Outlook 2013 中测试了这段代码,它对我有用。
-
您是否记得在打开 Outlook 时打开宏?
-
@OpiesDad:没有错误消息,只是没有任何反应。也许我没有看到或想到的某个地方的某些设置会是一个问题......不过,我在启动 Outlook 时肯定会打开宏。
-
如果您手动运行 Application_Startup 会发生什么?那它行得通吗?如果在 Deleted_Items_ItemAdd 中设置断点,它会到达断点吗?
标签: vba gmail google-apps outlook-2013