Outlook宏在一个线程中移动整个对话

时间:2019-07-12 19:32:08

标签: vba outlook outlook-vba

我编写了一个宏,当我将电子邮件标记为重要邮件时,该宏应该将我的电子邮件会话移至“ TO DO”文件夹。我发现移动功能确实发生了,但是我得到了一个副本(即该线程在我的“ TODO”文件夹中显示,并且仍然保留在“收件箱”中。

有趣的是,在这行代码“对于Conversation.GetRootItems中的每个MailItem”中,由于所有消息的确都移动了MailItem> 1,所以我期望如此,但是实际上,这段代码仅执行一次然后循环完成。关于如何做一个真正的举动而不是看起来像是复制品的想法?

''''
Public WithEvents GExplorer As Outlook.Explorer
Public WithEvents GMailItem As Outlook.MailItem
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
    Set GExplorer = Outlook.Application.ActiveExplorer 'IGNORE THIS'
    Dim olNameSpace As Outlook.NameSpace
    Dim olFolder  As Outlook.MAPIFolder

    Set olNameSpace = Application.GetNamespace("MAPI")
    Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox)
    Set Items = olFolder.Items
End Sub

Private Sub Items_ItemChange(ByVal Item As Object)
'this item/macro is used to move an email message once it has been flagged
    Dim olNameSpace As Outlook.NameSpace
    Dim olFolder  As Outlook.MAPIFolder
    Dim olInbox  As Outlook.MAPIFolder

    Set olNameSpace = Application.GetNamespace("MAPI")
    Set olFolder = olNameSpace.Folders("DEBUG").Folders("TODO")

    Stop 'THIS WAS FOR DEBUGGING
    If TypeOf Item Is Outlook.MailItem And Item.FlagStatus = olFlagMarked Then
        Set Conversation = Item.GetConversation

        If Not IsNull(Conversation) Then
            ' Set ItemsTable = conversation.GetTable

            'MsgBox Conversation.GetRootItems.Count

            For Each MailItem In Conversation.GetRootItems ' Items in the conversation. ONLY RUNS ONCE'
                If TypeOf MailItem Is Outlook.MailItem Then
                    Item.Move olFolder
                End If
            Next
        End If
    End If

End Sub
''''

3 个答案:

答案 0 :(得分:0)

这是因为对于更改的项目重复相同的动作(move),并将其作为参数传递给ItemChange事件处理程序。相反,您必须对循环中的item对象运行Move方法:

            For Each MailItem In Conversation.GetRootItems ' Items in the conversation. ONLY RUNS ONCE'
                If TypeOf MailItem Is Outlook.MailItem Then
                    MailItem.Move olFolder
                End If
            Next

答案 1 :(得分:0)

除了Eugene的建议(在调用std::array时使用MailItem而不是item之外),切勿在修改集合的循环中对Outlook对象使用Move“您正在遍历-使用下循环"for each

答案 2 :(得分:0)

也许您需要对话的GetChildren。