【发布时间】:2018-03-24 18:47:07
【问题描述】:
我正在寻找一种将不同 Outlook 帐户的信息下拉到 Excel 电子表格中的方法。
以下代码仅适用于我的个人收件箱:
Sub psinbox()
Dim olNs As Outlook.Namespace
Dim oltaskfolder As Outlook.MAPIFolder
Dim oltask As Outlook.TaskItem
Dim olitems As Outlook.Items
Dim xlapp As Excel.Application
Dim xlWB As Excel.Workbook
Dim x As Long
Dim arrheaders As Variant
Set olNs = GetNamespace("MAPI")
Set oltaskfolder = olNs.GetDefaultFolder(olFolderInbox)
Set olitems = oltaskfolder.Items
Set xlapp = CreateObject("Excel.Application")
xlapp.Visible = True
Set xlWB = xlapp.Workbooks.Add
x = 2
arrheaders = Array("Date Created", "Date Recieved", "Subject", "Sender",
"Senders Email", "CC", "Sender's Email Type", "MSG Size", "Unread?")
On Error Resume Next
xlWB.Worksheets(1).Range("A1").Resize(1, UBound(arrheaders)).Value = ""
Do
With xlWB.Worksheets(1)
If Not (olitems(x).Subject = "" And olitems(x).CreationTime = "") Then
.Range("A1").Resize(1, UBound(arrheaders) + 1) = arrheaders
.Cells(x, 1).Value = olitems(x).CreationTime
.Cells(x, 2).Value = olitems(x).recievedtime
.Cells(x, 3).Value = olitems(x).Subject
.Cells(x, 4).Value = olitems(x).SenderName
.Cells(x, 6).Value = olitems(x).CC
.Cells(x, 7).Value = olitems(x).SenderEmailType ' this is either internal or external server
.Cells(x, 8).Value = Format((olitems(x).Size / 1024) / 1024, "#,##0.00") & " MB"
.Cells(x, 9).Value = olitems(x).UnRead
x = x + 1
End If
End With
Loop Until x >= olitems.Count + 1
Set olNs = Nothing
Set oltaskfolder = Nothing
Set olitems = Nothing
Set xlapp = Nothing
Set xlWB = Nothing
End Sub
我想记录收到的邮件有多少未读。
我找到的最接近的是Count Read and Unread Emails date wise for shared mailbox,其中提到需要设置 c = b.Folders("共享邮箱的名称"),但这似乎是针对同一邮件帐户内的不同文件夹。我所追求的是访问 Outlook 可以访问的两个不同帐户?
编辑:
尝试了 Niton 的示例后,我遇到了以下问题。
If objOwner.Resolved Then
Set oltaskfolder = olNs.GetSharedDefaultFolder(objOwner,
olFolderInbox).Folders("admin")
Set olitems = oltaskfolder.Items
End If
我尝试使用共享收件箱的用户名、邮箱地址和邮箱账户名,但都出现以下错误。
【问题讨论】:
-
如果您的个人资料中没有邮箱stackoverflow.com/questions/27851850/…,否则这是您找到的问题的另一个版本stackoverflow.com/questions/9076634/…
-
以上是指子文件夹,而不是单独的邮件帐户。不过我会尝试你的第一个建议中的链接slipstick.com/developer/working-vba-nondefault-outlook-folders