对整个邮箱使用 AdvancedSearch 方法的范围

Scope using AdvancedSearch method for entire mailbox

我正在尝试在所有 Outlook 文件夹中查找与特定参数匹配的邮件项目以保存来自 Excel 的附件。

我不知道如何引用范围来遍历所有文件夹,甚至是自定义文件夹。

我找不到能回答我问题的资源。

Sub testing()

Dim myOlApp As New Outlook.Application
Dim scope As String
Dim filter As String
Dim rsts As Results
Dim AdvancedSearch As Outlook.Search

blnSearchComp = False

'I want it to search the entire mail account including normal folders like inbox and sent as well as custom folders.
'but this doesn't work. Any ideas?
scope = "'Fakeexample123@outlook.com'"
'filter assignment statement has been excluded

Set AdvancedSearch = myOlApp.AdvancedSearch(scope, filter, True, "test")  

While blnSearchComp <> True
    If AdvancedSearch.Results.Count > 0 Then
        blnSearchComp = True
    End If
Wend

Set rsts = AdvancedSearch.Results

For x = rsts.Count To 1 Step -1
    rsts.Attachment.Item(x).SaveAsFile Project
Next

End Sub

好吧,我知道这不是最好的解决方案,但我想出了以下代码来列出所有父文件夹,以便可以使用 for 循环和高级搜索方法来迭代列表。这不是最快的代码,但应该不会太慢。

Sub main()
'establishes connections
Dim myOlApp As New Outlook.Application
Dim objNS As Outlook.Namespace
Dim myFolder As Outlook.MAPIFolder
Set objNS = myOlApp.GetNamespace("MAPI")
'pick highest folder level as original my folder
Set myFolder = objNS.Folders("faxe.example123@outlook.com")

Call ProcessFolders(myFolder)

End Sub

Sub ProcessFolders(myFolder)

're establish connections
Dim myOlApp As New Outlook.Application
Dim objNS As Outlook.Namespace
Dim objFolder As Outlook.MAPIFolder
Set objNS = myOlApp.GetNamespace("MAPI")
'set up collection
Set x = New Collection

For Each objFolder In myFolder.Folders
'add all parent folder names to collection
'advanced search method will handle subfolders
'can use a recursive call here also to get subfolders though
    x.Add objFolder.Name
Next


Set objNS = Nothing
Set myFolder = Nothing
Set myOlApp = Nothing

End Sub

范围应该是

'\Fakeexample123@outlook.com'
Sub Demo_scopeformat()

Dim myOlApp As New outlook.Application
Dim scope As String

' Mailbox
scope = "'" & myOlApp.Session.GetDefaultFolder(olFolderInbox).Parent.folderPath & "'"

' Expected format
Debug.Print scope

End Sub