Excel VBA Outlook搜索多个条件(ID和日期)

时间:2017-03-01 18:38:00

标签: excel vba excel-vba outlook

好的,这是我对这个VBA搜索难题的第二个主题。感谢在我的第一期帮助我的那个人。此代码源自enter link description here

好吧所以我做了调整,让它搜索一个确实有用的SharedMailbox,但问题是邮箱每天收到数百封电子邮件,这使我的搜索时间变得更长(我们去年年初的电子邮件甚至)。我想强加第二个搜索条件,这次是一个日期限制,就像只搜索2到3天的电子邮件一样。这是我得到的:

Dim outlookapp
Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim olMail As Variant
Dim myTasks
Dim projIDsearch As String
Dim myRecipient As Outlook.Recipient
Dim days2ago As Date



Set outlookapp = CreateObject("Outlook.Application")
Set olNs = outlookapp.GetNamespace("MAPI")
Set myRecipient = olNs.CreateRecipient("SharedMailboxName")
myRecipient.Resolve

'Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("x")
Set Fldr = olNs.GetSharedDefaultFolder(myRecipient, olFolderInbox)




Set myTasks = Fldr.Items
projIDsearch = ActiveCell.Cells(1, 4)


days2ago = DateTime.Now - 3



For Each olMail In myTasks

'If olMail.ReceivedTime > days2ago Then


If (InStr(1, olMail.Subject, projIDsearch, vbTextCompare) > 0) Then
olMail.Display
'Exit For
End If


Next

我环顾四周,发现.ReceivedTime属性。这听起来像我需要的东西,但我正在努力如何将其融入代码。实际上我甚至不是Variant(olMail)如何能够接受.display方法和.subject属性。

这些是我添加的代码,但它们似乎不起作用:

days2ago = DateTime.Now - 3

If olMail.ReceivedTime > days2ago Then

先谢谢。

1 个答案:

答案 0 :(得分:1)

您可以限制循环中的项目数。 PYTHONUSERBASE

{{1}}