按月计算电子邮件数量

时间:2017-05-23 15:33:07

标签: excel vba excel-vba outlook

我修改了我的搜索电子邮件代码以进行计数。

Public Sub mycounter()
Dim outlookapp
Dim olns As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim olMail As Object
'Dim olMail As Outlook.MailItem
Dim myTasks
Dim projIDsearch As String
Dim myrecipient As Outlook.Recipient
Dim daysAgo As Long
Dim strfilter As String
Dim emailcount As Integer

Set outlookapp = CreateObject("Outlook.Application")
Set olns = outlookapp.GetNamespace("MAPI")

Set myrecipient = olns.CreateRecipient("SharedMailbox")
myrecipient.Resolve
Set Fldr = olns.GetSharedDefaultFolder(myrecipient, olFolderInbox)

daysAgo = CInt(ProjIDSearcher.ComboBox1.Text)

Set myTasks = Fldr.Items
Set myTasks = myTasks.Restrict("[ReceivedTime]>'" & Format(Date - daysAgo, "DDDDD HH:NN") & "'")

For Each olMail In myTasks
    emailcount = emailcount + 1
    'Exit For
Next

MsgBox "here it is:" & emailcount

End Sub

此代码按天数计算。有没有办法可以从下拉菜单中提取具体日期?

Set myTasks = myTasks.Restrict("[ReceivedTime]>'" & Format(Date - daysAgo, "DDDDD HH:NN") & "'")

现在,daysAgo正在从下拉列表中获取数字值。我需要计算特定月份/年份的电子邮件,以便我可以计算2017年1月或2016年5月收到的电子邮件数量。

1 个答案:

答案 0 :(得分:0)

我无法测试您的代码,但过滤器的构造允许检索给定日期范围内的项目,如下所示:

sFilter = ""
sFilter = "[ReceivedTime] >= '" & Format(StartDate, "ddddd h:nn AMPM") & "'"
sFilter = sFilter & " AND [ReceivedTime] <= '" & Format(EndDate, "ddddd h:nn AMPM") & "'"

过滤器的构建规则可以在Items.Restrict(filter)讨论的MSDN知识库中找到。

在上文中,请注意EndDate逻辑上需要在所需结束日期之后的第二天,假设您希望计数包含EndDate(或者您可以使用EndDate +1和<运算符)。 / p>