我想在信箱里闪现一下昨天收到的邮件数量。
我目前拥有的代码是:
Public Sub YesterdayCount()
Set ns = Application.GetNamespace("MAPI")
Set outApp = CreateObject("Outlook.Application")
Set outNS = outApp.GetNamespace("MAPI")
Dim Items As Outlook.Items
Dim MailItem As Object
Dim yestcount As Integer
yestcount = 0
Set Folder = outNS.Folders("Correspondence Debt Advisor Queries").Folders("Inbox")
Set Items = Folder.Items
For Each Item In Items
If MailItem.ReceivedTime = (Date - 1) Then
yestcount = yestcount + 1
End If
Next
MsgBox yestcount
End Sub
问题出在线路上:
If MailItem.ReceivedTime = (Date - 1) Then
错误提示没有设置对象变量,但我在研究之后无法理解这一点。
发布于 2016-02-10 22:14:25
你差一点就成功了。基本上,您从未设置过MailItem
,也没有将其限定为Item
,而且由于ReceivedTime
是日期/时间格式,因此它永远不会等于一个直接的Date
。
请参阅下面的代码。我添加了一些按ReceivedTime
排序的功能,一旦循环超过昨天的日期,就退出循环。我还清理了一些变量命名,这样就不会与固有的Outlook对象命名约定混淆。
Public Sub YesterdayCount()
Dim outNS As Outlook.NameSpace
Set outNS = Application.GetNamespace("MAPI")
Dim olFolder As Outlook.Folder
Set olFolder = outNS.Folders("Correspondence Debt Advisor Queries").Folders("Inbox")
Dim olItems As Outlook.Items
Set olItems = olFolder.Items
olItems.Sort "[ReceivedTime]", True
Dim yestcount As Integer
yestcount = 0
Dim item As Object
For Each item In olItems
'commented code works for MailItems
'Dim olMail As Outlook.MailItem
'Set olMail = item
Dim dRT As Date
'dRT = olMail.ReceivedTime
dRT = item.ReceivedTime
If dRT < Date And dRT > Date - 2 Then
If dRT < Date - 1 Then Exit For
yestcount = yestcount + 1
End If
Next
MsgBox yestcount
End Sub
https://stackoverflow.com/questions/35326813
复制