如何在阅读后关闭时自动将收件箱邮件保存到文件夹 - Outlook 2010 VBA

时间:2015-02-12 21:47:04

标签: email outlook-vba outlook-2010 inbox

我想将此保留在VBA中。我正在寻找有关如何解决以下问题的信息。

我收到此错误: 项目的属性和方法不能在此事件过程中使用。 MS已阻止人们在Inspector_Close事件中使用.Close,.Move和.Delete方法。 我已经看到了使用线程来运行延迟宏的建议,但无法在此找到帮助,并怀疑它可能在VBA中不可用。

我的代码如下:

Private Sub objInspector_Close()
Dim objNS As NameSpace
Dim objFolder As MAPIFolder
'On Error Resume Next
Set objNS = Application.Session

If Not mailSent Then
    If objInspector.CurrentItem.Class = olMail Then
        'Mail inspector is closing
        If objInspector.CurrentItem.Parent = "Inbox" Then   
            Set objFolder = objNS.PickFolder
            If Not objFolder Is Nothing And IsInDefaultStore(objFolder) _
               And objFolder.DefaultItemType = olMailItem Then
                Set objInspector.CurrentItem.Move = objFolder
            End If
        End If
    End If
Else
    mailSent = False
End If

Set objFolder = Nothing
Set objNS = Nothing
End Sub

全局mailSent布尔值用于防止在发送/关闭电子邮件时执行此事件代码。

错误发生在 Set objInspector.CurrentItem.Move = objFolder 上。 有没有办法让我延迟这个直到事件结束,或者可能在电子邮件项目上设置一些标志,然后在我的收件箱中的所有电子邮件上运行宏以将它们移动到文件夹。

我处理多个项目并维护多个电子邮件文件夹,并且正在寻找自动化电子邮件管理的方法。我已经看过其他页面,其中文件夹名称来自电子邮件主题,但我不想这样做。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您可以考虑添加可以标记要移动的邮件等的用户属性。然后,您可以使用Find / FindNextRestrict方法搜索已标记的项目。您可以在以下文章中阅读有关这些方法的更多信息:

此外,您可以使用Folder类的GetTable方法获取包含Filter过滤的项目的Table对象。

您可能知道Outlook对象模型无法从其他线程使用。您需要使用低级API - 支持辅助线程的扩展MAPI。或者围绕该API的任何其他第三方包装器,例如 - Redemption。

答案 1 :(得分:0)

您可以放弃使用触发器并“手动”移动的想法

Option Explicit

Private Sub MoveCurrentItem()

Dim objNS As Namespace
Dim objFolder As folder
Dim currItem As Object
Dim uPrompt As String

Set objNS = Application.Session

On Error Resume Next
Set currItem = ActiveInspector.currentItem
On Error GoTo 0
If currItem Is Nothing Then GoTo ExitRoutine

If currItem.Class = olMail Then
    If currItem.Sent Then ' reading not composing
        If currItem.Parent = "Inbox" Then
            Set objFolder = objNS.PickFolder
            If Not objFolder Is Nothing And IsInDefaultStore(objFolder) _
               And objFolder.DefaultItemType = olMailItem Then
                currItem.Move objFolder
            End If
        End If
    End If
End If

ExitRoutine:
    Set currItem = Nothing
    Set objFolder = Nothing
    Set objNS = Nothing

End Sub