如何使“setAlwaysMoveConversation”正常工作?

时间:2016-12-01 21:15:15

标签: vba outlook outlook-vba

在Outlook中,如果我激活“始终在此对话中移动邮件”,则会:

  1. 将对话中的所有邮件移至目标文件夹,包括已发送邮件
  2. 中的邮件
  3. 从那一刻开始,该对话中收到的所有邮件都将移至目标文件夹。但是,该对话中已发送的所有邮件都将保留在已发送邮件文件夹中。
  4. 我希望第1步排除那些已发送的项目。

    背景:我们正在使用共享邮箱,我不能为我们每个人快速迈出一步,因为它们会有太多。 所以我创建了一个子按钮,它带有用户名并移动(启用总是移动)到相应的文件夹。

    但是,我希望保留已发送的项目 - 这是可能的,还是应该创建自己的“alwaysMoveMessages”函数?

    谢谢!

1 个答案:

答案 0 :(得分:2)

使用Conversation.GetRootItems一个SimpleItems集合,其中包含对话的根项目或所有根项目,以及Conversation.GetTable包含对话中所有项目的Table对象。

示例代码

Option Explicit
Sub MoveConv()
    Dim olNs As NameSpace
    Dim Inbox As Outlook.MAPIFolder
    Dim SubFolder As Outlook.MAPIFolder
    Dim SelectedItem As Object
    Dim Item As Outlook.MailItem ' Mail Item
    Dim Folder As Outlook.MAPIFolder ' Current Item's Folder
    Dim Conversation As Outlook.Conversation ' Get the conversation
    Dim ItemsTable As Outlook.Table ' Conversation table object
    Dim MailItem As Object

    Set olNs = Application.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)

'    On Error GoTo MsgErr
'   // Must Selected Item.
    Set SelectedItem = Application.ActiveExplorer.Selection.Item(1)

'   // If Item = a MailItem.
    If TypeOf SelectedItem Is Outlook.MailItem Then
        Set Item = SelectedItem
        Set Conversation = Item.GetConversation

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

            For Each MailItem In Conversation.GetRootItems ' Items in the conv.
                If TypeOf MailItem Is Outlook.MailItem Then
                    Set Item = MailItem
                    Set Folder = Item.Parent
                    Set SubFolder = Inbox.Folders("Temp") ' Move to Temp Folder
                    Debug.Print Item.ConversationID & " In Folder " & Folder.Name
                    GetConv Item, Conversation
                    Item.Move SubFolder
                End If
            Next
        End If
    End If

MsgErr_Exit:
    Set olNs = Nothing
    Set Inbox = Nothing
    Set Item = Nothing
    Set SelectedItem = Nothing
    Set MailItem = Nothing
    Exit Sub

'// Error information
MsgErr:
    MsgBox "Err." _
         & vbCrLf & "Error Number: " & Err.Number _
         & vbCrLf & "Error Description: " & Err.Description _
         , vbCritical, "Error!"
    Resume MsgErr_Exit
End Sub

Function GetConv(Item As Object, Conversation As Outlook.Conversation)
    Dim Items As Outlook.SimpleItems
    Dim MailItem As Object
    Dim Folder As Outlook.Folder
    Dim olNs As NameSpace
    Dim Inbox As Outlook.MAPIFolder
    Dim SubFolder As Outlook.MAPIFolder

    Set olNs = Application.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
    Set Items = Conversation.GetChildren(Item)

    If Items.Count > 0 Then
        For Each MailItem In Items
            If TypeOf MailItem Is Outlook.MailItem Then
                Set Item = MailItem
                Set Folder = Item.Parent
                Set SubFolder = Inbox.Folders("Temp")
                Debug.Print Item.ConversationID & " In Folder " & Folder.Name
                Item.Move SubFolder
            End If
            GetConv Item, Conversation
        Next
    End If
End Function