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

时间:2015-07-11 00:37:57

标签: excel-vba scope advanced-search vba excel

所以我是dasl查询的新手,并且一直在搜索网络,试图找出如何正确使用它们。我试图通过所有outlook文件夹查看匹配某些参数的邮件项目,并保存附件...非常简单。

我很遗憾,我的代码存在问题,我不知道如何引用范围来浏览所有文件夹,甚至是自定义文件夹。

下面是我的代码(我意识到一些陈述是错误的),任何帮助表示赞赏。我已经对这些问题进行了尽职调查,但找不到回答我问题的资源,这就是我在这里发帖的原因。任何答案或答案的参考将不胜感激。

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

如果需要任何其他信息,请告诉我们,再次感谢您的帮助! =)

PS:感谢编辑提示Byron

2 个答案:

答案 0 :(得分:0)

嗯,我知道它不是最好的解决方案,但我已经提出以下代码来列出所有父文件夹,以便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

答案 1 :(得分:0)

范围应为

'\\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