1
votes

I wrote a program in Excel 2010 to sift through emails in a selected Outlook 2010 folder, and pull in information from the email (html) body.

I updated to Office 2016. Since then, I get an error when using certain properties of the MailItem object. I can pull the subject of the email into Excel, but certain properties cause a "method 'body' of object'_mailItem" failed error (including the .Body and .To properties).

Below is a simplified version of the code:

Sub GatherInfo()

Dim ObjOutlook As Object
Dim MyNamespace As Object
Dim FormFolder As Object

Set ObjOutlook = GetObject(, "Outlook Application")
Set MyNamespace = ObjOutlook.GetNamespace("MAPI")
Set FormFolder = MyNamespace.PickFolder

For i = 1 To FormFolder.Items.Count

    Range("A2").Select
    ActiveCell.Value = FormFolder.Items(i).Subject
    ActiveCell.Offset(0, 1).Value = FormFolder.Items(i).To

End Sub

This results in:

Run-time error '-2147467259(80004005)':
Method 'To' of object'_MailItem' failed

I've done some research, and wondering if Outlook 2016 security settings could be to blame. This is a corporate email account, running on an exchange server. Do you think that could be preventing me from accessing the body/sender of the email?

It's strange that the subject property of the email works, but not the body/to properties.

Things I've ruled out:

1) I've sent both plain text and html based emails with the same result.
2) I've tried binding the Outlook objects early (Dim ObjOutlook as Outlook.Application, etc.)

I ensured there were only mail items and no calendar items, etc.

It'll trip out the first time it hits the item.To assignment. If I insert a line to resume next then it'll go through all the emails, but will only record the subject and not the .To property.

1
Outlook 2016 for mac or windows?0m3r
This answer suggests starting in Outlook stackoverflow.com/a/58964879niton

1 Answers

1
votes

Avoid using multiple dot notation and check if you really have a MailItem object (you can also have ReportItem or MeetingItem):

set items = FormFolder.Items
For i = 1 To items.Count
  set item = items.Item(i)
  if item.Class = 43 Then
    Range("A2").Select
    ActiveCell.Value = item.Subject
    ActiveCell.Offset(0, 1).Value = item.To
  End If
  set item = Nothing
next
set items = Nothing