2

I am writing a script in Outlook VBA to record every email in an Access database as they come in to my inbox. The code I have triggers with no issue. It accesses the Access database with no issue. It copies the subject across with no issue. Then it gets to the body and copies nothing at all. I have tried things like .HTMLbody instead of just .Body, but this again shows an empty body. My code is as follows:

Option Explicit
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim objNS As Outlook.NameSpace
Dim objEmail As Outlook.MailItem
Dim strIDs() As String
Dim intX As Integer
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim sDb As String
Dim sSQL As String
Dim qdf As QueryDef
strIDs = Split(EntryIDCollection, ",")
For intX = 0 To UBound(strIDs)
Set objNS = Application.GetNamespace("MAPI")
Set objEmail = objNS.GetItemFromID(strIDs(intX))
    sDb = "C:\Users\######\Documents\EmailDatabase.accdb"
    Set ws = DBEngine.Workspaces(0)
    Set db = ws.OpenDatabase(sDb)
    sSQL = "INSERT INTO AllEmails (Subject,Message) Values ('" & objEmail.Subject & "','" & objEmail.HTMLBody & "')"
    Set qdf = db.CreateQueryDef("", sSQL)
    qdf.Execute dbFailOnErro
MsgBox objEmail.HTMLBody
Next
Set objEmail = Nothing
End Sub

If anyone has any idea what I am doing wrong please do let me know. Three hours of googling doesn't seem to have sorted it!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
codeacker
  • 85
  • 1
  • 11
  • Is your message box also returning nothing? – Jordan Feb 20 '17 at 15:51
  • Yes @Jordan the message box opens (so no fault thrown) but with nothing in it. – codeacker Feb 20 '17 at 15:52
  • Step through your code (F8) and view `objEmail` in your Locals window after it's assigned. See if there is even a `body` property when you expand the variable. Wondering if `GetItemFromID` is actually returning a `MailItem` – click here Feb 20 '17 at 15:54
  • Just tried that @click, there is a `body` property set to an empty string. I can see the subject string and everything else fine, but no `body`. Also forgot to mention before, this is on an IMAP account, does that make any difference? – codeacker Feb 20 '17 at 16:19
  • The same question was asked previously http://stackoverflow.com/questions/23601992/unable-to-read-outlook-mailbody-using-application-newmailex-event A comment there hints that the body that should be loaded if requested, may not yet be loaded . – niton Feb 20 '17 at 20:53
  • Thanks for the suggestion @niton I have made sure my outlook settings download the message and attachments immediately, to no avail. Otherwise this would seem to explain the problem. – codeacker Feb 20 '17 at 20:58

2 Answers2

1

This question has now been resolved thanks to LEBoyd! The solution is to .display the message and then immediately .close olDiscard. For some yet-to-be-explained reason, it fills the body. See LEBoyd's question here - Outlook 2010 Email body is empty

codeacker
  • 85
  • 1
  • 11
0

Try to remove any extra code (Access) from the NewMailEx event handler. Use this method with caution to minimize the impact on Outlook performance. However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously. You should not assume that after these events fire, you will always get a one-item increase in the number of items in the Inbox.

The EntryIDsCollection string contains the Entry ID that corresponds to that item. Note that this behavior has changed from earlier versions of the event when the EntryIDCollection contained a list of comma-delimited Entry IDs of all the items received in the Inbox since the last time the event was fired.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • That's enlightening thanks @eugene your link to MSDN explains that NewMailEx is for POP3 accounts not IMAP. So maybe the best method would be create a rule to run a script when an email is received? Then how would I identify that email? – codeacker Feb 20 '17 at 20:51
  • An instance of the `MailItem` is passed to the VBA macro in that case. There is no need to use `GetItemFromId` method for rules. – Eugene Astafiev Feb 20 '17 at 21:10
  • I tried this, it again gets the subject with no issue, but not the body. That means it cannot be downloading the message until it is previewed or opened. Now I am going to try triggering it at that point... more Googling needed :) – codeacker Feb 21 '17 at 20:58
  • @codeacker, I know this is old but did you ever find a solution to your problem? I am having a similar issue on 2010 Outlook. The subject is there, HTMLBody shows some HTML text but not text from the message and .Body is empty. I tried looping through a Sleep process because I found one post about a possible delay in loading .Body, but even after a 20 second loop I still have no .Body. – LEBoyd Aug 01 '18 at 14:12
  • @LEBoyd I didn't find a satisfactory solution this way, so I set a rule in Outlook to run a macro when an email was received, and the macro simply launches a python script which fetches the last item in the imap account. It works well for me, but not sure if that option will be open to you? – codeacker Aug 01 '18 at 19:54
  • 1
    Thanks @codeacker. I did find that if I .display the message and then immediately .close oldiscard, for whatever reason it fills the body. This was the new question I asked: https://stackoverflow.com/questions/51639665/outlook-2010-email-body-is-empty?noredirect=1#comment90247654_51639665 – LEBoyd Aug 02 '18 at 12:13