将AdvancedSearch for Outlook与C#一起使用将返回零结果 [英] Using AdvancedSearch for Outlook with C# Returns Zero Results

查看:98
本文介绍了将AdvancedSearch for Outlook与C#一起使用将返回零结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在收件箱和所有子文件夹中搜索主题行中的给定字符串.我在网上找到了以下代码(

I'm trying to search my inbox and all subfolders for a given string in the subject line. I found the following code online(https://www.add-in-express.com/creating-addins-blog/2012/05/31/outlook-search-csharp-vbnet/), but it returns zero results which is not the expected result.

我查看了Outlook中视图设置下针对给定搜索词的过滤器,该搜索词在Outlook Explorer中返回结果,并得到了以下查询:"

I looked at the filter under view settings in outlook for a given search term that returns results in the outlook explorer and got this query: "http://schemas.microsoft.com/mapi/proptag/0x0037001f" LIKE '%Ticket%' When I plug that in to the below code I likewise get zero results.

当我使用LINQ查询那些文件夹时(LINQ太慢而不能成为一个真正的解决方案),我可以获得结果,所以我猜测我正在使用advancedsearch进行语法错误.很难在网络上找到使用示例.我会感谢任何可以帮助我的人.

When I use LINQ to query those folders(LINQ is too slow to be a real solution here) I can get results so I'm guessing I'm making a syntactical error with advancedsearch. It is hard to find examples of usage on the web. I will appreciate anyone that can help me.

        private Search RunAdvancedSearch(Outlook._Application OutlookApp, string wordInSubject)
        {
        string advancedSearchTag = "New Search";
        string scope = "Inbox";
        string filter = "\"urn:schemas:mailheader:subject\" LIKE '%"+ wordInSubject +"%'";

        Outlook.Search advancedSearch = null;
        Outlook.MAPIFolder folderInbox = null;
        Outlook.MAPIFolder folderSentMail = null;
        Outlook.NameSpace ns = null;
        try
        {
            ns = OutlookApp.GetNamespace("MAPI");
            folderInbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            folderSentMail = ns.GetDefaultFolder(
                                           Outlook.OlDefaultFolders.olFolderSentMail);
            scope = "\'" + folderInbox.FolderPath +
                                          "\',\'" + folderSentMail.FolderPath + "\'";
            advancedSearch = OutlookApp.AdvancedSearch(
                                            scope, filter, true, advancedSearchTag);
            System.Diagnostics.Debug.WriteLine(advancedSearch.Results.Count);

        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message, "An exception is thrown!");
        }
        finally
        {
            if (advancedSearch != null) Marshal.ReleaseComObject(advancedSearch);
            if (folderSentMail != null) Marshal.ReleaseComObject(folderSentMail);
            if (folderInbox != null) Marshal.ReleaseComObject(folderInbox);
            if (ns != null) Marshal.ReleaseComObject(ns);
        }


        return advancedSearch;
    }

推荐答案

我等待的时间不够长. AdvancedSearch(在单独的线程中运行)完成后,将触发一个名为AdvancedSearchComplete的事件.我不得不告诉代码来处理事件,以便等待搜索完成.

I was not waiting long enough for the results. When AdvancedSearch(which runs in a separate thread) is finished it fires off an event called AdvancedSearchComplete. I had to tell the code to handle the event in order to wait for the search to complete.

在RunAdvancedSearch中,我在尝试"中执行此操作:

In RunAdvancedSearch I do this in the Try with this:

Application.AdvancedSearchComplete += Application_AdvancedSearchComplete;

这就是全部.

    string advancedSearchTag = "MY FOOFOO Search";
    //SEARCH Function
    Search RunAdvancedSearch(Outlook.Application Application, string wordInSubject)
    {
        string scope = "Inbox";
        string filter = "urn:schemas:mailheader:subject LIKE \'%" + wordInSubject + "%\'";
        Outlook.Search advancedSearch = null;
        Outlook.MAPIFolder folderInbox = null;
        Outlook.MAPIFolder folderSentMail = null;
        Outlook.NameSpace ns = null;
        try
        {
            ns = Application.GetNamespace("MAPI");
            folderInbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            folderSentMail = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
            scope = "\'" + folderInbox.FolderPath + "\',\'" +
                                                       folderSentMail.FolderPath + "\'";
            advancedSearch = Application.AdvancedSearch(
                                                scope, filter, true, advancedSearchTag);
            Application.AdvancedSearchComplete += Application_AdvancedSearchComplete;
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message, "An eexception is thrown");
        }
        return advancedSearch;
        finally
        {
            if (advancedSearch != null) Marshal.ReleaseComObject(advancedSearch);
            if (folderSentMail != null) Marshal.ReleaseComObject(folderSentMail);
            if (folderInbox != null) Marshal.ReleaseComObject(folderInbox);
            if (ns != null) Marshal.ReleaseComObject(ns);
        }

    }
    //Handle AdvancedSearchComplete event
    void Application_AdvancedSearchComplete(Outlook.Search SearchObject)
    {
        Outlook.Results advancedSearchResults = null;
        Outlook.MailItem resultItem = null;
        System.Text.StringBuilder strBuilder = null;
        try
        {
            if (SearchObject.Tag == advancedSearchTag)
            {
                advancedSearchResults = SearchObject.Results;
                System.Diagnostics.Debug.WriteLine("Count: " + advancedSearchResults.Count);
                if (advancedSearchResults.Count > 0)
                {
                    strBuilder = new System.Text.StringBuilder();
                    strBuilder.AppendLine("Number of items found: " +
                                            advancedSearchResults.Count.ToString());
                    foreach (MailItem item in advancedSearchResults)
                    {
                        System.Diagnostics.Debug.WriteLine(item.Subject);
                    }
                    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 Date: " +
                                                    resultItem.SentOn.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.");
                }
            }
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message, "An exception is occured");
        }
        finally
        {
            if (resultItem != null) Marshal.ReleaseComObject(resultItem);
            if (advancedSearchResults != null)
                Marshal.ReleaseComObject(advancedSearchResults);
        }
    }
  private void btnOutlookSrch_Click(object sender, EventArgs e)
    {
       Outlook.Application OLook = new Outlook.Application();
       RunAdvancedSearch(OLook, "Hello?");
    }

这篇关于将AdvancedSearch for Outlook与C#一起使用将返回零结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆