【问题标题】:Reference inbox of a different account参考其他帐户的收件箱
【发布时间】: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

我尝试使用共享收件箱的用户名、邮箱地址和邮箱账户名,但都出现以下错误。

【问题讨论】:

标签: excel vba email outlook


【解决方案1】:

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

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

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

编辑:

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

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

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

【讨论】:

    猜你喜欢
    • 2012-02-23
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2016-07-15
    • 2017-05-21
    相关资源
    最近更新 更多