outlook宏高级搜索

时间:2017-04-26 15:36:48

标签: vba outlook outlook-vba

我想创建一个宏来对任务主题和可选的任务主体执行“高级搜索”。例如搜索“@Cris”

我已经从Excel的示例中复制并修改了此代码,但它没有运行。运行时错误。感谢任何帮助

SELECT * FROM your_table
WHERE UNIX_TIMESTAMP(DateVisited) >= UNIX_TIMESTAMP(CAST(NOW() - INTERVAL 1 DAY AS DATE))
  AND UNIX_TIMESTAMP(DateVisited) <= UNIX_TIMESTAMP(CAST(NOW() AS DATE));

2 个答案:

答案 0 :(得分:0)

您需要指定有效的范围和搜索条件。搜索范围是文件夹的文件夹路径,而不是文件夹名称。建议将文件夹路径括在单引号中。否则,如果文件夹路径包含包含Unicode字符的特殊字符,则搜索可能无法返回正确的结果。要指定多个文件夹路径,请将每个文件夹路径用单引号括起来,并用逗号分隔单个带引号的文件夹路径。

Filter参数可以是任何有效的DASL查询。有关DASL查询的其他信息,请参阅Filtering Items

注意,您可以使用Chr Function来表示搜索条件中的任何字符。

Sub TestSearchForMultipleFolders()
 Dim Scope As String
 Dim Filter As String
 Dim MySearch As Outlook.Search
 Dim MyTable As Outlook.Table
 Dim nextRow As Outlook.Row
 m_SearchComplete = False
 'Establish scope for multiple folders
 Scope = "'" & Application.Session.GetDefaultFolder( _
olFolderInbox).FolderPath _
 & "','" & Application.Session.GetDefaultFolder( _
 olFolderSentMail).FolderPath & "'"
 'Establish filter
 If Application.Session.DefaultStore.IsInstantSearchEnabled Then
    Filter = Chr(34) & "urn:schemas:httpmail:subject" _
    & Chr(34) & " ci_phrasematch 'Office'"
 Else
    Filter = Chr(34) & "urn:schemas:httpmail:subject" _
    & Chr(34) & " like '%Office%'"
 End If
 Set MySearch = Application.AdvancedSearch(Scope, Filter, True, "MySearch")
 While m_SearchComplete <> True
    DoEvents
 Wend
 Set MyTable = MySearch.GetTable
  Do Until MyTable.EndOfTable
    Set nextRow = MyTable.GetNextRow()
    Debug.Print nextRow("Subject")
 Loop
End Sub

另外,您可能会发现Advanced search in Outlook programmatically: C#, VB.NET文章很有帮助。

答案 1 :(得分:0)

好的,这就是我的工作。

启动宏时:

  1. 它要求用户输入字符串
  2. 执行并提前搜索并搜索主题中的字符串(包括@cris等特殊字符)
  3. 创建搜索文件夹以显示搜索结果
  4. 如果它可以帮助其他人。我不知道如何在进行手动搜索时创建输出。但这对我有用。

    Sub AdvSearchForStr()
        On Error GoTo Err_SearchFolderForSender
        Dim strFrom As String
        Dim strTo As String
        Dim strSearch As String
    
        strSearch = InputBox("Enter String to AdvSearch", "Advanced Search")
    
        strTo = "Test"
    
        Dim strDASLFilter As String
        strDASLFilter = "urn:schemas:httpmail:subject LIKE '%" & strSearch & "%'"
    
        Debug.Print strDASLFilter
    
        Dim strScope As String
        strScope = "'Inbox', 'Sent Items', 'Tasks'"
    
        Dim objSearch As Search
        Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder")
    
        'Save the search results to a searchfolder
        objSearch.Save (strSearch)
    
        Set objSearch = Nothing
        Exit Sub
    
    Err_SearchFolderForSender:
        MsgBox "Error # " & Err & " : " & Error(Err)
    
    End Sub