问题
I am facing a "Run-time error 287" when accessing Outlook emails via Excel VBA. However, I'm getting this message only when run the script in Office 2013, with an Exchange account assigned to my Outllok client. When running in Office 2019, with my personal Outlook.com email address assigned to the client, it runs fine, doing what is expected to be done.
Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olMailItem As Outlook.MailItem
Dim rng As Range
Dim i As Long
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox).Folders("CV")
Set rng = Range("table1")
i = 1
For Each olMailItem In olFolder.Items
rng.Cells(i, 1).value = olMailItem.ReceivedTime 'Success
rng.Cells(i, 2).value = olMailItem.subject 'Success
rng.Cells(i, 3).value = olMailItem.Sender 'Throw error 287
rng.Cells(i, 3).value = olMailItem.SenderName 'Throw error 287
rng.Cells(i, 4).value = Left(olMailItem.Body, 20) 'Throw error 287
i = i + 1
Next olMailItem
Set olFolder = Nothing
Set olNamespace = Nothing
Set olApp = Nothing
MsgBox (i - 1) & " emails retrieved."
I have already checked on the libraries references, but their exactly the same in both scenarios I tested.
Therefore, I'm afraid it's something related to the Exchange account or even the Office version, but both hypotheses are still strange, since I can still access some of the MailItem
properties.
Does anyone has any idea what it could be?
来源:https://stackoverflow.com/questions/54576277/run-time-error-287-when-retrieving-outlook-email-properties-from-excel