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

rwurgleyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David LeeCommented:
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.
0
KarenAnalyst programmerCommented:
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
0
rwurgleyAuthor Commented:
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
0
KarenAnalyst programmerCommented:
hmmm... okay I think it must be a security issue, since Body is one of the "protected" properties, although I would have expected a dialog box to appear from Outlook warning you that "another application is trying to access your emails" or whatever.

If you get the same error when accessing any of the following properties, then we know that it is a security issue:
Bcc, Body, Cc, HTLMBody, PropertyAccessor, ReceivedByName, ReceivedOnBehalfOfName, Recipients, ReplyRecipientNames, SenderEmailAddress, SenderEmailType, SenderName, SentOnBehalfOfName, To

I dont think there is an easy way to get around it, except for just turning off all warnings in Outlook. See
http://www.outlookcode.com/article.aspx?ID=52
for ways to overcome the problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.