2017-02-25 177 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对您有帮助。