0

I have some VBA that checks the subject of every message as soon as it hits my inbox, and submits certain emails' contents over http to a server for processing.

This works great for messages with no attachments, but fails if there is an attachment on the email. I am using a http GET to submit the text.

What is the effect of the presence of an attachment on the body property of the message, and how can I ignore the attachment and submit only the email body text?

The VBA (trimmed for clarity but complete and functional):

Declarations:

Option Explicit
Private WithEvents olInboxItems As Items

On startup:

Private Sub Application_Startup()
    Set olInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

On item added to inbox:

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)

    On Error Resume Next

    Dim olMailItem As MailItem
    Dim strAttachmentName As String
    Dim submitResult As String

    If TypeOf Item Is MailItem Then
        Set olMailItem = Item

        If ((InStr(olMailItem.subject, "test subject") > 0)
            Dim subject As String
            subject = olMailItem.subject
            Dim contents As Variant
            contents = olMailItem.body
            Dim submitURL As String
            submitURL = "http:// ... " & subject & "..." & contents & "..."
            XMLHttpSynchronous submitURL
        End If
    End If
    Set Item = Nothing
    Set olMailItem = Nothing
End Sub

Http submit:

Sub XMLHttpSynchronous(ByVal URL As String)
    Dim XMLHttpRequest As XMLHTTP

    Set XMLHttpRequest = New MSXML2.XMLHTTP

    XMLHttpRequest.Open "GET", URL, False
    XMLHttpRequest.Send

End Sub

edit: I am now stripping attachments with the below code (tested and working) but the url still isn't submitting correctly.

    Set myattachments = olMailItem.Attachments
    While myattachments.Count > 0
        myattachments.Remove 1
    Wend

edit 2: I set contents=subject and the url submitted correctly, still no luck with the email's body, even after the message has been stripped of attachments

nightTrevors
  • 639
  • 4
  • 11
  • 24
  • The body property should be a string, however perhaps HTML or RTF characters are what's actually messing around with your URL? Maybe try to strip out the attachments (ie olMainItem.Attachments) before sending to your server? – Marcin Jan 02 '14 at 13:06
  • OK I might be wrong here but doesn't Get allow only 256 characters in a URL? That might be one thing, and MailItem.Body doesn't contain any contents related to attachments. It purely contains the contents of the email. So it is difficult to believe that it is attachments which are causing the problem here. I agree with Marcin that the problem might be special characters or the length of the string here. – Vikas Jan 02 '14 at 13:28
  • Hey @Marcin, I added Set myattachments = olMailItem.Attachments While myattachments.Count > 0 myattachments.Remove 1 Wend and it correctly stripped the attachment but still didn't submit the email's body. Thanks for your suggestion – nightTrevors Jan 02 '14 at 13:31
  • @Vikas the body of the email is less than 256 characters – nightTrevors Jan 02 '14 at 13:33
  • What is the error you are getting? – Vikas Jan 02 '14 at 13:35
  • The VBA doesn't raise an error. The code will continue to work in the same session with the non-attachment emails, so I agree, it must be an issue with the url – nightTrevors Jan 02 '14 at 13:36
  • I would say if you are not getting any error in VBA, is there something at the server end which is causing the problem and skipping? Just saying. I am out of ideas, sorry :( – Vikas Jan 02 '14 at 13:39

1 Answers1

0

In order to get this working I stripped the attachments with the code in my first edit, than split the string of the body's text around the first and last character. I'm not sure why this works

nightTrevors
  • 639
  • 4
  • 11
  • 24