[在Stack Overflow上的几篇文章的帮助下,我为Outlook编写了此宏,该宏将计算本周到达的特定文件夹中的邮件数,将未读邮件与该周的邮件总数进行比较。
问题是它运行速度确实很慢,并且该文件夹中当前少于100条消息。真的很慢= Outlook会暂停几秒钟,然后弹出带有结果的消息框。
我假设有一种更有效的方法来执行此操作。你能指出我正确的方向吗?
我当前的代码:
Dim Vfolder As Outlook.Folder
Set Vfolder = Application.Session.Folders("abc").Folders("123")
Dim x As Long ' Unread messages in folder from this week
Dim y As Long ' Total messages in folder from this week
Dim i As Long ' loop
Dim objMail As Outlook.MailItem
Dim DateReceived As Date
Dim FullDateReceived As Date
Dim DateTest As Date
DateTest = Date - Weekday(Date, vbMonday) + 1
For i = 1 To Vfolder.Items.Count
If Vfolder.Items.Item(i).Class = olMail Then
Set objMail = Vfolder.Items.Item(i)
FullDateReceived = objMail.ReceivedTime
DateReceived = Year(FullDateReceived) & "-" & Month(FullDateReceived) & "-" & Day(FullDateReceived)
If DateReceived >= DateTest Then
If objMail.UnRead Then
x = x + 1
y = y + 1
Else
y = y + 1
End If
End If
End If
Next
all项-毕竟,如果没有WHERE子句,您永远不会运行SQL查询,对吗?
更糟糕的是,您正在使用多个点符号(Vfolder.Items.Item(i)
),这会使OOM在循环的每次迭代中返回一个全新的Items对象。使用Items.Find/FindNext
或Items.Restrict
。在您的特定情况下,它将很简单,例如
dt = Now-7
strDate = FormatDateTime(dt, 2)
set vfolder = Application.ActiveExplorer.CurrentFolder
y = Vfolder.Items.Restrict("[ReceivedTime] > '" & strDate & "'").Count
x = Vfolder.Items.Restrict("([Unread] = true) and ([ReceivedTime] > '" & strDate & "')").Count