如何使用vba从整个邮箱(所有文件夹和存档)搜索电子邮件主题

问题描述 投票:0回答:2

我想从整个 Outlook(包括所有文件夹和存档)中搜索电子邮件主题。 我是否要更改 getdefaultfolder 或者我应该做什么?

Sub SubjectFound()
Dim oOutlook As Object
Dim oInbox As Object
Dim oFilter As Object
Dim oNS As Object
Dim sFilter As String

Dim lr As Long, r As Long
Const olFolderInbox = 6

Set oOutlook = CreateObject("outlook.application")
Set oNS = oOutlook.GetNamespace("MAPI")
Set oInbox = oNS.getdefaultfolder(olFolderInbox)

lr = Range("A" & Rows.Count).End(xlUp).Row
For r = 2 To lr
    sFilter = "@SQL=""urn:schemas:httpmail:subject"" = '" & Range("A" & r).Value & "'"
    sFilter = sFilter & " AND "
    sFilter = sFilter & "%today(""urn:schemas:httpmail:datereceived"")%"
    Set oFilter = oInbox.items.restrict(sFilter)
    
    Range("B" & r) = IIf(oFilter.Count > 0, "Task Completed", "Task Incomplete")
    DoEvents
Next
Set oOutlook = Nothing
Set oNS = Nothing
Set oInbox = Nothing
Set oFilter = Nothing
End Sub
excel vba outlook
2个回答
0
投票

使用 Application.AdvancedSearch - 它允许搜索多个文件夹及其子文件夹。请记住,搜索是异步的,您需要等待它完成和/或使用 Results 对象事件。


0
投票

使用 Outlook

AdvancedSearch
类的
Application
方法,该方法根据指定的 DAV 搜索和定位 (DASL) 搜索字符串执行搜索。

在 Outlook 中使用

AdvancedSearch
方法的主要好处是:

  • 搜索是在另一个线程中执行的。您不需要手动运行另一个线程,因为
    AdvancedSearch
    方法会在后台自动运行它。
  • 可以在任何位置(即超出特定文件夹的范围)搜索任何项目类型:邮件、约会、日历、笔记等。
    Restrict
    Find
    /
    FindNext
    方法可应用于特定的
    Items
    集合(请参阅 Outlook 中
    Items
    类的
    Folder
    属性)。
  • 完全支持 DASL 查询(自定义属性也可用于搜索)。为了提高搜索性能,如果商店启用了即时搜索,则可以使用即时搜索关键字(请参阅
    IsInstantSearchEnabled
    类的
    Store
    属性)。
  • 您可以使用
    Stop
    类的
    Search
    方法随时停止搜索过程。

在我为技术博客撰写的以编程方式在 Outlook 中进行高级搜索:C#、VB.NET 中阅读有关该方法的更多信息,您可以在其中找到代码示例和可能用途的更详细说明。

© www.soinside.com 2019 - 2024. All rights reserved.