Can I modify Conversation ID in Outlook by VBA to group independent Emails?

前端 未结 2 835
南方客
南方客 2021-02-20 08:56

I do receive a lot of mails sent by various robots. I can identify Emails easily by subject (for example: \"Response to ticket 123\"). Unfortunately each email is generated aut

相关标签:
2条回答
  • 2021-02-20 09:11

    I was able to solve this myself by using Redemption to get write-access to the MAPI properties of ConversationTopic and ConversationIndex.

    While apparently ConversationTopic is used at first for grouping messages, also ConversationIndex plays a role in grouping: It does not only carry the sorting time stamp, but the first bytes are a conversation code that must match across all emails of a conversation. Otherwise they are still not grouped, even with same topic. See here for details: https://msdn.microsoft.com/en-us/library/ms528174(v=exchg.10).aspx

    Luckily, setting the Index to Null apparently makes Outlook pay attention only to the topic, so we don't need to re-calculate a new index.

    My working code:

    Dim oNS As Object
    Dim oRDOSess As Object
    Dim oRDOItem As Object
    
    Debug.Print "Creating Redemption Object ..."
    ' This requires: http://www.dimastr.com/redemption/download.htm
    Set oRDOSess = CreateObject("Redemption.RDOSession")
    Set oNS = Nothing
    Set oNS = Outlook.GetNamespace("MAPI")
    oNS.Logon
    oRDOSess.MAPIOBJECT = oNS.MAPIOBJECT
    
    Set oRDOItem = oRDOSess.GetMessageFromID(incomingMail.EntryID, incomingMail.Parent.StoreID)
    
    Debug.Print "Trying to change conversation topic ..."
    oRDOItem.ConversationTopic = incomingMail.Subject
    
    Debug.Print "Trying to change conversation index ..."
    oRDOItem.Fields("http://schemas.microsoft.com/mapi/proptag/0x00710102") = Null
    
    Debug.Print "Saving modified mail item ..."
    oRDOItem.Save
    
    0 讨论(0)
  • 2021-02-20 09:14

    This is not a complete answer, but it is too long for a comment.

    I was able to set the MAPI conversationTopic and conversationIndex properties using the tips here and the code:

     oItem.propertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x00710102", _
                 oItem2.propertyAccessor.GetProperty("http://www.slipstick.com/developer/read-mapi-properties-exposed-outlooks-object-model/")
    

    for the ConversationIndex property, for example. This assumes that you have one message as oItem and the other as oItem2, both declared as Objects. Note that this property is binary, and so if you want to look at it you can use:

     oItem2.propertyAccessor.BinaryToString(x)
    

    where x represents the property (set to a variable or just put the propertyAccessor.GetProperty code in there). This becomes relevant because the ConversationID of the message object is the last bunch of "characters/binary bits" of the MAPI ConversationIndex property. However, changing the ConversationIndex property DID NOT change the ConversationID.

    Both the ConversationIndex and conversationTopic properties of the message object are read-only, however changing the conversationTopic MAPI property DID change the ConversationTopic property of the message. I was unable to get this to actually group the messages, however.

    My research suggested that the ConversationTopic property should be the one that initially groups the messages, with the ConversationIndex property sorting them after the grouping, but, as I mentioned, I was unable to get the messages to group, even after assigning the same ConversationTopic to both MAPI and the message object.

    Here's code that helps show this behavior:

    Dim Msg As Outlook.MailItem
    Dim oItem As Object
    Dim oItem2 As Object
    Dim objNS As Outlook.NameSpace
    Dim olFolder As Outlook.MAPIFolder
    Dim Item As Object
    
    Set objNS = GetNamespace("MAPI")
    Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
    
    
    For Each Item In olFolder.Items
        If TypeName(Item) = "MailItem" Then
            Debug.Print "Subject: " & Item.Subject & " " & Item.propertyAccessor.BinaryToString(Item.propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x00710102"))
    
            If Item.Subject = "test" Then
                Set oItem = Item
            ElseIf Item.Subject = "test2" Then
                Set oItem2 = Item
            End If
        End If
    Next Item
    
    Debug.Print "OItem: " & vbCr _
                & "ConversationIndex: " & oItem.ConversationIndex & vbCr _
                & "ConversationID: " & oItem.ConversationID & vbCr _
                & "ConversationTopic: " & oItem.ConversationTopic & vbCr _
                & "MAPI ConversationIndex: " & oItem.propertyAccessor.BinaryToString(oItem.propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x00710102")) & vbCr _
                & "MAPI ConversationTopic: " & oItem.propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0070001E") & vbCr
    
    
    Debug.Print "OItem2: " & vbCr _
                & "ConversationIndex: " & oItem2.ConversationIndex & vbCr _
                & "ConversationID: " & oItem2.ConversationID & vbCr _
                & "ConversationTopic: " & oItem2.ConversationTopic & vbCr _
                & "MAPI ConversationIndex: " & oItem2.propertyAccessor.BinaryToString(oItem2.propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x00710102")) & vbCr _
                & "MAPI ConversationTopic: " & oItem2.propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0070001E") & vbCr
    
    Debug.Print "Set OItem2 To OItem"
    
    oItem2.propertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x0070001E", oItem.ConversationTopic
    oItem2.propertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x00710102", oItem.propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x00710102")
    
    Debug.Print "OItem: " & vbCr _
                & "ConversationIndex: " & oItem.ConversationIndex & vbCr _
                & "ConversationID: " & oItem.ConversationID & vbCr _
                & "ConversationTopic: " & oItem.ConversationTopic & vbCr _
                & "MAPI ConversationIndex: " & oItem.propertyAccessor.BinaryToString(oItem.propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x00710102")) & vbCr _
                & "MAPI ConversationTopic: " & oItem.propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0070001E") & vbCr
    
    
    Debug.Print "OItem2: " & vbCr _
                & "ConversationIndex: " & oItem2.ConversationIndex & vbCr _
                & "ConversationID: " & oItem2.ConversationID & vbCr _
                & "ConversationTopic: " & oItem2.ConversationTopic & vbCr _
                & "MAPI ConversationIndex: " & oItem2.propertyAccessor.BinaryToString(oItem2.propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x00710102")) & vbCr _
                & "MAPI ConversationTopic: " & oItem2.propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0070001E") & vbCr
    

    Sharing this in case it helps anyone solve the problem.

    0 讨论(0)
提交回复
热议问题