Link to home
Start Free TrialLog in
Avatar of rwurgley
rwurgley

asked on

Problem reading Outlook 2007 email body from VB

Hey all,

I'm having a problem with reading the body of an email with VB in outlook 2007. I've added a small piece of the code i'm trying to use as an example to the question. To simplify things, all i have it doing right now is reading and msgbox'ing the subject and body of the emails. In outlook 2003 this worked without issue but now for some reason its not working on outlook 2007. I can view and msgbox the subject without any issue, but when i go to view the message body i'm getting an error that i haven't been able to figure out. It's most likely something simple that i'm doing wrong but no matter what i try i'm still getting the same error. From what i can see, .body is part of the mailitem object, and i know its able to find the emails since its able to display the subjects without any problem. I hope someone can see what i'm doing wrong here. If there is anything you need more clarification, please feel free to ask and i will try and provide any info I can.

Error msg: Method 'Body' of object '_MailItem' failed


Thanks for your assistance,
Roy

Error msg: Method 'Body' of object '_MailItem' failed
Dim db As DAO.Database
Dim OlApp As outlook.Application
Dim Alerts As outlook.MAPIFolder
Dim strEmail As outlook.MailItem

Set db = CurrentDb
Set OlApp = CreateObject("Outlook.Application")
Set olNs = OlApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set Alerts = Fldr.Folders("Alerts")


If Alerts.Items.Count > 0 Then
    For i = Alerts.Items.Count To 1 Step -1
        Set strEmail = Alerts.Items(i)
        MsgBox strEmail.Subject
        MsgBox strEmail.Body
        
    Next
End If

Open in new window

Avatar of David Lee
David Lee
Flag of United States of America image

Hi, rwurgley.

Body is definitely a valid property name.  Have you tried setting a breakpoint on line 16 and using the debugger to see what what properties are available?  Body should be one, but I'm curious what you'll see.
The items collection does not necessarily only contain MailItem items. Try:

    For i = Alerts.Items.Count To 1 Step -1
        If Alerts.Items(i).Class = olMail Then
            Set strEmail = Alerts.Items(i)
            MsgBox strEmail.Subject
            MsgBox strEmail.Body
        End If
    Next
Avatar of rwurgley
rwurgley

ASKER

Sorry for the delay. It's been hectic the past couple days.

Blue: when i look at the available properties, body is definately one of them. along with a lot more involving the email such as subject, attachments, senton, receivedon, etc.

snow: I'm added a screenshot so you can see, but its still the same result. It's obviously seeing that it is a mail item but just won't touch body.


Just to see what would happen, i added some more properties to see if it was more of them that won't work, or just body. All of them work without issue except for body.

Do you think it may be a security related issue where outlook won't let access touch body? I would think it would prevent it from viewing all of it, and not just body though. The emails are not signed, or encrypted so that isn't a factor either.

Any other ideas? Thanks for the help so far. its appreciated.

MsgBox strEmail.Subject
MsgBox strEmail.BodyFormat
MsgBox strEmail.Class
MsgBox strEmail.SentOn
MsgBox strEmail.ReceivedTime
MsgBox strEmail.Size

Open in new window

error-debug.JPG
ASKER CERTIFIED SOLUTION
Avatar of Karen
Karen
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial