我正在尝试在共享邮箱中提取Outlook邮件的详细信息。
为了让同事们更轻松,我在Excel中创建了宏。
它落在了:
sender = olItem.sender
在Outlook VBA中,它会提取电子邮件发件人的姓名。
在Excel中它会导致
运行时错误287应用程序定义或用户定义错误
每条消息都会发生这种情况。如果我删除/注释掉错误的代码行,那么它会成功地提取主题行并收到日期和时间。
Sub GetEmailData()
Dim olApp As Outlook.Application
Dim NS As Outlook.Namespace
Dim Inbox As Outlook.MAPIFolder
Dim objOwner As Outlook.Recipient
Dim olItem As Outlook.MailItem
Dim subject As String
Dim sender As String
Dim senddate As String
Dim homebook As Workbook, homesheet As Worksheet
Set homebook = Workbooks("Email Log.xlsm")
Set homesheet = homebook.Worksheets("Sheet1")
'Clear contents
homesheet.Range("A4:D100").Delete
Set olApp = Outlook.Application
Set NS = olApp.GetNamespace("MAPI")
Set objOwner = NS.CreateRecipient("***email address of mailbox***")
Set Inbox = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
n = 4
For Each olItem In Inbox.Items
subject = olItem.subject
sender = olItem.sender
senddate = olItem.ReceivedTime
homesheet.Range("B" & n).Value = subject
homesheet.Range("C" & n).Value = sender
homesheet.Range("A" & n).Value = senddate
n = n + 1
Next
homesheet.Range("A1").Value = "Last Updated: " & Now()
homesheet.Range("A2").Value = "Number of Items: " & Inbox.Items.Count
End Sub