3

I've read this: VBA Outlook 2010 received mail .Body is empty but it is old and the other question referenced in the answer(s) is not found when I click on it.

Here's my basic code.

Sub AutoReplyTrap(objInMail As MailItem)
    
    Dim objOutMail As Outlook.MailItem
    
    Dim vText As Variant
    Dim sText As String
    Dim strID As String
    Dim sSubject As String
    Dim vItem As Variant
    Dim vFirstName As Variant
    Dim i As Long
    Dim j As Integer
    Dim strSignature As String
    Dim strSigString As String
    Dim strFirstName As String
    Dim strFirstLetter As String
    Dim strEMailAddress As String
    Dim blnFirstName As Boolean
    Dim blnEMail As Boolean
    
    ' change the bodyformat to plain text
    objInMail.BodyFormat = Outlook.OlBodyFormat.olFormatPlain
    
    objInMail.Display

    blnFirstName = False
    blnEMail = False
    j = 0
    
    ' believe there is a timing issue that Body may not be fully loaded.
    ' so I'm going to pause and loop through 20 times to see if it gets loaded.
WaitForBody:
    sText = objInMail.Body
    If sText = "" Then
        If j < 20 Then
            j = j + 1
            Sleep 1000
            GoTo WaitForBody
        End If
    End If
    
    If sText = "" Then
        MsgBox ("No body in email!")
        Exit Sub
    End If

End Sub

I thought it was a timing issue, so I built the loop to test if I have the body, and if not, wait a second and try again up to 20 times.

I have objInMail.Display it works, but if I remove that line it will loop through the 20 attempts.

I could live with the display if I could then "un-display" it, but I wonder if the .close will close everything with the email and I'll lose the body again.

I'd prefer it to work without the objInMail.Display.

niton
  • 8,771
  • 21
  • 32
  • 52
LEBoyd
  • 151
  • 12
  • I did find that objInMail.Close olDiscard immediately after the .Display will close the message window and I do get the body. If I comment out the .Display or both statements .Display and .Close, the .Body is empty. Any help is appreciated. – LEBoyd Aug 01 '18 at 18:36
  • So what do you want to do once you change the body format? – 0m3r Aug 01 '18 at 20:17
  • @0m3r, not sure that it makes a different, but what I do is pull some data from the incoming message, create a new message with the pulled data and send the new message. – LEBoyd Aug 02 '18 at 12:11

2 Answers2

0

Ignoring the cause, this may provide a workaround without .Display.

Option Explicit

Private Sub test_GetInspector()

    Dim currSel As Object
    Set currSel = ActiveExplorer.Selection(1)

    If currSel.Class = olMail Then
        AutoReplyTrap_GetInspector currSel
    End If

End Sub

Sub AutoReplyTrap_GetInspector(objInMail As mailItem)

    ' change the bodyformat to plain text
    objInMail.BodyFormat = OlBodyFormat.olFormatPlain

    ' objInMail.GetInspector    ' Previously "valid".
    ' My setup finally caught up and provided the clue.
    ' Directly replacing .Display with .GetInspector
    '  Compile error:
    '  Invalid use of property

    ' https://learn.microsoft.com/en-us/office/vba/api/outlook.mailitem.getinspector
    Dim objInspector As Inspector
    Set objInspector = objInMail.GetInspector

    ' You should find this is necessary
    'objInMail.Save

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52
0

Working with Outlook 2010 right now and have an update. The issue is caused by a bug in Outlook 2010/2013 that only gives a blank message body in VBA when:

(1) using IMAP protocol; and, (2) automatically processing incoming emails.

This holds true even if you just set a Rule from the front end, such as automatically printing specific incoming emails (my task). This prints the email header, not the body.

A workaround that worked for me was to use POP3 protocol instead of IMAP with the same email server.