用于按日期计算Outlook共享收件箱中项目的Excel宏

时间:2017-10-25 23:17:35

标签: excel vba excel-vba outlook

我正在尝试在Excel中为我们的某个团队编写宏来按日期计算特定共享Outlook邮件文件夹中的项目数,并在Excel中显示计数。

到目前为止,我已经能够计算我的个人收件箱(检索邮箱的不同代码)并且我已经能够访问共享邮箱文件夹以获得可用项目的总数(检索邮箱使用下面的代码)。

现在我的问题是我得到运行时错误'438':对象不支持此属性或方法。

我已将其跟踪到使用iCount的代码的数组部分。

非常感谢任何寻找参考或帮助代码的方向。

Sub HowManyDatedEmails()

' Set Variables
Dim EmailCount As Integer, DateCount As Integer, iCount As Integer
Dim myDate As Date
Dim arrEmailDates()

Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olFldr As Outlook.MAPIFolder
Dim olItem As Object
Dim olMailItem As Outlook.MailItem

'Call Folder
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFldr = olNS.Folders("delivery quality team")
Set olFldr = olFldr.Folders("Inbox")


' Put ReceivedTimes in array
EmailCount = olFldr.Items.Count
For iCount = 1 To EmailCount
    With olFldr.Items(iCount)
        ReDim Preserve arrEmailDates(iCount - 1)
        arrEmailDates(iCount - 1) = DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime))
    End With
Next iCount

' Clear Outlook objects
Set olFldr = Nothing
Set olNS = Nothing
Set olApp = Nothing

' Count the emails dates equal to active cell
Sheets("Count_Data").Range("A2").Select
Do Until IsEmpty(ActiveCell)

    DateCount = 0
    myDate = ActiveCell.Value

    For i = 0 To UBound(arrEmailDates) - 1
        If arrEmailDates(i) = myDate Then DateCount = DateCount + 1
    Next i

    Selection.Offset(0, 1).Activate
    ActiveCell.Value = DateCount
    Selection.Offset(1, -1).Activate
Loop
End Sub

2 个答案:

答案 0 :(得分:0)

执行以下操作:

Dim inItem As Outlook.MailItem
iCount = 0
For Each inItem In olFldr.Items
    iCount = iCount + 1
    With inItem
        ReDim Preserve arrEmailDates(iCount - 1)
        arrEmailDates(iCount - 1) = DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime))
    End With
Next inItem

答案 1 :(得分:0)

一般情况下,我建议您使用Find / FindNextRestrict方法,而不是迭代文件夹中的所有项目。请阅读以下文章中有关这些方法的更多信息:

  

现在我的问题是我得到运行时错误'438':对象不支持此属性或方法。

事实上,Outlook文件夹可能包含不同的项目类型 - 邮件,备注,日历项目等。因此,很可能您正在处理的项目类型没有在代码中指定的属性。在循环中,我首先建议项目类型,然后处理其属性。有关详细信息,请参阅When is a MailItem not a MailItem?