2017-02-25 223 views
0

我正在處理Outlook 2013中的插件。我向MailItem添加了自定義字段並使用AdvancedSearch查找項目。最後一個缺失的部分是顯示結果。如何在Outlook搜索中顯示搜索結果

如何顯示搜索結果中的自定義搜索結果?

private void Application_AdvancedSearchComplete(Outlook.Search SearchObject) 
    { 
     string dx = SearchObject.Tag; 
     int x = SearchObject.Results.Count; 
     //What next? 
    } 

    private void button1_Click(object sender, RibbonControlEventArgs e) 
    { 
     Object selObject = Globals.ThisAddIn.Application.ActiveExplorer().Selection[1]; 
     Outlook.MailItem mail = selObject as Outlook.MailItem; 
     if (mail != null) 
     { 
      Outlook.UserProperties mailUserProperties = null; 
      Outlook.UserProperty mailUserProperty = null; 
      mailUserProperties = mail.UserProperties; 
      foreach (var i in mailUserProperties) 
      { 
       var xx = i; 
      } 
      mailUserProperty = mailUserProperties.Add("TestUserProperty", Outlook.OlUserPropertyType.olText, true); 
      mailUserProperty.Value = "Eugene Astafiev"; 
      mail.Save(); 
     } 

     string str = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/TestUserProperty LIKE '%ugene%'"; 
     Outlook.MAPIFolder inbox = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); 
     Globals.ThisAddIn.Application.AdvancedSearch("Inbox", str, false, "TestUserProperty"); 
    } 

回答

0

您無法顯示Application.AdvancedSearch的結果。要在用戶界面中顯示搜索結果,您必須使用Explorer.Search,它實際上只是在UI中自動執行搜索。但是,您將無法將結果返回到代碼中進行處理。請參閱此處以瞭解您的選項概述: https://msdn.microsoft.com/en-us/library/ff869846.aspx

0

您可以將結果保存到搜索文件夾中,然後將其顯示給用戶。 Search類的Save方法將搜索結果保存到搜索文件夾中。請注意,如果已存在具有相同名稱的搜索文件夾,Save方法將顯示錯誤。

 Outlook.Results advancedSearchResults = advancedSearch.Results; 
     if (advancedSearchResults.Count > 0) 
     { 
      if (HostMajorVersion > 10) 
      { 
       object folder = advancedSearch.GetType().InvokeMember("Save", 
            System.Reflection.BindingFlags.Instance | 
            System.Reflection.BindingFlags.InvokeMethod | 
            System.Reflection.BindingFlags.Public, 
            null, advancedSearch, 
            new object[] { advancedSearchTag }); 

      } 
      else 
      { 
       strBuilder = new System.Text.StringBuilder(); 
       strBuilder.AppendLine("Number of items found: " + 
          advancedSearchResults.Count.ToString());        
       for (int i = 1; i < = advancedSearchResults.Count; i++) 
       {         
        resultItem = advancedSearchResults[i] 
             as Outlook.MailItem; 
        if (resultItem != null) 
        { 
         strBuilder.Append("#" + i.ToString()); 
         strBuilder.Append(" Subject: " + resultItem.Subject); 
         strBuilder.Append(" \t To: " + resultItem.To); 
         strBuilder.AppendLine(" \t Importance: " + 
              resultItem.Importance.ToString()); 
         Marshal.ReleaseComObject(resultItem); 
        } 
       } 
       if (strBuilder.Length > 0) 
        System.Diagnostics.Debug.WriteLine(strBuilder.ToString()); 
       else 
        System.Diagnostics.Debug.WriteLine(
              "There are no Mail items found."); 
      } 
     } 
     else 
     { 
      System.Diagnostics.Debug.WriteLine("There are no items found."); 
     } 

您可能會發現文章Advanced search in Outlook programmatically: C#, VB.NET對您有幫助。