通过Excel VBA访问Outlook电子邮件时,我遇到“运行时错误287”。但是,只有在Office 2013中运行脚本并将Exchange帐户分配给我的Outllok客户端时,我才会收到此消息。在Office 2019中运行时,将我的个人Outlook.com电子邮件地址分配给客户端,它运行良好,可以完成预期的工作。
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."
我已经检查了库引用,但是在我测试的两种情况下它们完全相同。
因此,我担心这与Exchange帐户甚至Office版本有关,但是由于我仍然可以访问某些MailItem
属性,因此这两个假设仍然很奇怪。
有人知道它可能是什么吗?