另一个帐户的参考收件箱

时间:2017-10-12 18:08:49

标签: excel vba email outlook

我正在寻找一种方法将不同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

我尝试使用共享收件箱的用户名,电子邮件地址和电子邮件帐户的名称,但都会出现以下错误。

Current Error

1 个答案:

答案 0 :(得分:0)

答案似乎是删除导致并发症的部分。

If objOwner.Resolved Then
    Set oltaskfolder = olNs.GetSharedDefaultFolder(objOwner, 
olFolderInbox)
    Set olitems = oltaskfolder.Items
End If

删除.Folders(" admin")修复了即将发生的错误并解决了问题。然后它完全按照要求向我提供了有关收件箱的信息。

修改

我刚刚发现的旁注,如果你想在共享邮箱中有一个子文件夹,只需在olFolderInbox旁边添加.Folders(" mailbox"),如下所示。 / p>

If objOwner.Resolved Then
    Set oltaskfolder = olNs.GetSharedDefaultFolder(objOwner, 
olFolderInbox).Folders("mailbox")
    Set olitems = oltaskfolder.Items
End If

之前的页面无法在CreateRecipient旁边添加它吗?