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.