views:

179

answers:

2

Responding to an email with the subject line "test", with this code...

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
  If TypeName(Item) = "MailItem" Then
    Debug.Print Item.Subject
    Debug.Print Item.Parent
  End If
End Sub

...returns this.

Inbox
RE: test

I'm looking to get to "test", which is the email being responded to so it can be automatically .Move(d) to an archive folder.

+2  A: 

This would be better in Outlook 2010, I think. For earlier versions, I believe you want this code which is quoted directly from http://www.outlookcode.com/codedetail.aspx?id=1714

Function FindParentMessage(msg As Outlook.MailItem) _
           As Outlook.MailItem
    Dim strFind As String
    Dim strIndex As String
    Dim fld As Outlook.MAPIFolder
    Dim itms As Outlook.Items
    Dim itm As Outlook.MailItem
    On Error Resume Next
    strIndex = Left(msg.ConversationIndex, _
                    Len(msg.ConversationIndex) - 10)
    Set fld = Application.Session.GetDefaultFolder(olFolderInbox)
    strFind = "[ConversationTopic] = " & _
              Chr(34) & msg.ConversationTopic & Chr(34)
    Set itms = fld.Items.Restrict(strFind)
    Debug.Print itms.Count
    For Each itm In itms
        If itm.ConversationIndex = strIndex Then
            Debug.Print itm.To
            Set FindParentMessage = itm
            Exit For
        End If
    Next
    Set fld = Nothing
    Set itms = Nothing
    Set itm = Nothing
End Function
Remou
Remou. You didn't happen to try this code out did you? I can't get it to work and it's almost impossible to debug as it seems to throw errors in random ways. Wondering if it's just me.
GollyJer
Yes, I did try it, but in Outlook 2010 beta. I have an earlier version, but it is not available until tomorrow (WET, UTC+0)
Remou
Okay, in Outlook 2000, the ConversationIndex is stored in a different format, so the code will not suit 2000 as it stands. I am looking for more information. BTW what error are you getting?
Remou
I was able to get this working perfectly. Thanks Remou. The weird problems were being caused by Visual Studio 2010. The Outlook code would throw random "with variable not set errors" (and other things) even though it wasn't in a with statement. Then VS starting trying to install something every time I opened the Outlook VBE. As VS 2008 is working for me at this point I uninstalled 2010 and the VBA code started working as expected.
GollyJer
Good stuff. char
Remou
+2  A: 
Item.ConversationTopic

is the property you're looking for.

Dick Kusleika
Ultimately that was the property but Remou's answer got to the full job done. Thanks dick.
GollyJer
Agreed. And it's a good answer. I thought it would nice to separate the wheat from chaff in case anyone else just needs the quick hitter.
Dick Kusleika