我创建了宏来过滤具有给定编号的Outlook电子邮件,然后检查是否有任何电子邮件计数(如果是),则检查: -电子邮件主题->电子邮件正文->电子邮件发件人 如果一切正常,则返回该电子邮件已经发送。
可悲的是,由于if语句我的宏运行得非常慢,有什么方法可以加快它的速度吗?
没有其他方法可以进行过滤,因为此自动电子邮件每天发送大约65k的邮件。 在Outlook帐户上,我有2封电子邮件,这就是为什么必须选择它 我已经通过删除不需要的if语句使我的代码更快了-仍然可能会删除检查电子邮件发件人的最后一条语句,但首先需要删除所有不可靠的电子邮件
Dim last_number As Long
last_number = ThisWorkbook.Worksheets(2).Cells(Rows.Count, 2).End(xlUp).Row 'last number to check
Set Ns = myOlApp.GetNamespace("MAPI")
Set olSharedName = Ns.CreateRecipient("e-mail@adress.pl") 'pointing e-mail adress
Set Folder = Ns.GetSharedDefaultFolder(olSharedName, olFolderInbox)
Set numery = ThisWorkbook().Sheets(2).Range(Cells(2, 2), Cells(last_number, 2)) 'range of numbers to check
For Each number In numery
textToFind = number
strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:textdescription" & Chr(34) & " like '%" & textToFind & "%'" 'creating text to filter
Set filteredItems = Folder.Items.Restrict(strFilter) 'filtering by number
If filteredItems.Count = 0 Then
number.Offset(0, 7) = "No"
Else
x = 0
For Each OutMail In filteredItems
number.Offset(0, 8 + x) = OutMail.Subject
x = x + 1
If InStr(1, OutMail.Subject, "TEXT", vbTextCompare) <> 0 Then 'check if subject has desired "text" in it
If InStr(1, OutMail.Body, numer, vbTextCompare) <> 0 Then 'Check if e-mail has desired number in it
If InStr(1, OutMail.Sender, "E-mail sender", vbTextCompare) <> 0 Then 'Check if e-mail sender is correct = this will be probably deleted after cleaning of this inbox
number.Offset(0, 7) = "Tak"
GoTo new_number
End If
End If
End If
Next OutMail
number.Offset(0, 7) = "There were e-mail but without correct topic" ' information that there were e-mail but not with correct subject
End If
new_number:
Next number
感谢帮助。
答案 0 :(得分:0)
使用Restrict
方法应用过滤器非常有效。但是,如果以后有多个条件,则不能在消息正文(在示例代码中,由其DAV命名空间引用的Body属性)中搜索具有特定关键字的项目。我建议将它们添加到过滤器字符串中,并在以后不使用大量If
语句的情况下精确搜索此类项目。例如:
strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:textdescription" & Chr(34) & " like '%" & textToFind & "%' AND " & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%TEXT%'"
您可以在以下文章中详细了解Find
/ FindNext
和Restrict
方法:
此外,您可能会发现Application
类的AdvancedSearch方法,它具有以下优点:
有关更多信息,请参见Advanced search in Outlook programmatically: C#, VB.NET。