如何在Outlook搜索中显示搜索结果

时间:2017-02-25 09:40:31

标签: c# outlook vsto

我正在处理Outlook 2013中的插件。我向MailItem添加了自定义字段,并使用AdvancedSearch查找项目。最后一个缺失的部分是显示结果。

如何在搜索结果中显示自定义搜索的结果?

    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");
    }

2 个答案:

答案 0 :(得分:0)

您无法显示Application.AdvancedSearch的结果。要在UI中显示搜索结果,您必须使用Explorer.Search,这实际上只是自动化用户在UI中执行搜索。但是,您无法将结果返回到代码中以便使用。有关选项的概述,请参见此处:  https://msdn.microsoft.com/en-us/library/ff869846.aspx

答案 1 :(得分: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文章很有帮助。