Peter Peter - 1 month ago 14
C# Question

How to read only new content in VSTO Outlook MailItem body?

I've written a little C# VSTO add-in for Outlook 2003 that reads the body of emails as they are being sent, looking for certain words. It's working right now to do this:

if (currentItem.Body.Contains("text to search for"))

... but that checks the entire email body, not just the new message being sent.

Is there anyway to have Outlook just check the contents of the new message being sent, and so ignore the older email chain that might be in there too?

These messages could be in any format (HTML, Rich Text, Plain Text) and may or may not have any earlier messages chained in. This is just a productivity tool for me, so any hack that works is worth considering here.



For the record, the solution I decided worked best for me was to simply check for a line starting with "FROM:", Mikael suggested. I stop searching for my text as soon as that is found. That's been working just fine for me for a while now.

Thanks for the responses and ideas, everyone.

Here's my code for reference:

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    Dim theLine As String
    Dim aBody()
    Dim bFound As Boolean
    Dim ctr As Long

    aBody = Array(Split(Item.Body, vbNewLine))
    bFound = False

    For ctr = 0 To UBound(aBody(1))
        theLine = aBody(1)(ctr)
        If InStr(theLine, "From:") > 0 Then
            Exit For
        End If

        If InStr(UCase(theLine), "ATTACH") > 0 Then
            bFound = True
        End If


    If bFound Then
        If Item.Attachments.Count < 1 Then
            Dim ans As Integer

            ans = MsgBox("Do you really want to send this without any attachments?", vbYesNo)
            If ans = 7 Then
                Cancel = True
                Exit Sub
            End If
        End If
    End If

End Sub