Outlook AdvancedSearch创建搜索文件夹过滤异常问题求助
Outlook VBA宏创建搜索文件夹过滤问题
我经常需要搜索指定日期的所有邮件,为避免每次手动修改搜索文件夹条件,编写了Outlook VBA宏。该宏会询问目标日期后创建对应的搜索文件夹并自动显示。
宏可以正常运行,但存在两个问题:
- 手动创建的搜索文件夹仅显示当日邮件,宏创建的版本却会包含同事共享日历中当日的无关约会
- 宏创建的搜索文件夹,属性面板里的「修改条件」按钮处于禁用状态
我认为需要给AdvancedSearch方法添加额外过滤条件,但不知道具体该用哪些。当前宏代码如下:
Sub CreateSearchFolderForDate() 'Creates a search folder for a specific date. Only the primarey exchange mailbox will be considered '(no offline folders, no shared folders). 'The folder is displayed afterwards Dim oSearch As Search Dim oSearchFolder As Object Dim strScope As String Dim strFilter As String Dim strDate1 As String Dim strDate2 As String Dim strInput As String varInput = InputBox("Date?", "Create search order for a specific date", Date) If Not IsDate(varInput) Then Exit Sub End If 'Delete existing folder first, otherwise there is a runtime error Set oSearchFolder = GetSearchFolderByName("Mails for day X") If Not oSearchFolder Is Nothing Then oSearchFolder.Delete End If strScope = "'" & Application.Session.GetDefaultFolder(olFolderInbox).Parent.FolderPath & "'" strFilter = "urn:schemas:mailheader:date >= '" & CDate(varInput) & "' AND urn:schemas:mailheader:date < '" & CDate(varInput) + 1 & "'" Set oSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strFilter, SearchSubFolders:=True, Tag:="Mails of a specific date") oSearch.Save ("Mails for day X") Set oSearchFolder = GetSearchFolderByName("Mails for day X") oSearchFolder.Display End Sub Function GetSearchFolderByName(strSearchFolderName As String) As Object 'Returns the search folder with the display name specified. Only the primarey exchange mailbox will be considered '(no offline folders, no shared folders). Dim oStore As Outlook.Store Dim oFolder As Outlook.folder On Error Resume Next Set GetSearchFolderByName = Nothing For Each oStore In Application.Session.Stores If oStore.ExchangeStoreType = olPrimaryExchangeMailbox Then For Each oFolder In oStore.GetSearchFolders If oFolder.Name = strSearchFolderName Then Set GetSearchFolderByName = oFolder Exit Function End If Next End If Next End Function
我尝试过用urn:schemas:calendar:dtstart作为额外过滤条件,但要么无效,要么仅显示那些不想要的约会条目;使用IS NULL/IS NOT NULL会触发VBA运行时错误。也尝试过通过文件夹名称过滤,但找不到可用的属性。
2023-02-08 更新:我用Redemption工具分析了手动创建的搜索文件夹,得到对应的SQL语句,但尝试转换成VBA过滤语句时未成功,添加特定属性条件会触发运行时错误。现寻求正确的过滤条件建议。
内容的提问来源于stack exchange,提问作者Bootes
相关产品推荐
相关产品推荐

