Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

vba - How do I make a "setAlwaysMoveConversation" that works properly?

In Outlook, if I activate "always move messages in this conversation", it will:

  1. Move all of the messages in the conversation to the target folder, including those in Sent Items
  2. From that moment on, all messages received in that conversation will be moved to the target folder. However, all messages sent in that conversation will remain in the Sent Items folder.

I want step 1 to exclude those already in sent items.

Background: we're using a shared mailbox, and I can't have a quick step for each of us because there will be too many of them. So I made a sub with a button that takes the username and moves (enables always move) to the corresponding folder.

But, I want the sent items to remain - is this possible, or should I make my own "alwaysMoveMessages" function?

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Work with Conversation.GetRootItems A SimpleItems collection that includes the root item or all root items of the conversation and Conversation.GetTable A Table object that contains all Items in the conversation.

Example Code

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...