【问题标题】:Using AdvancedSearch for Outlook with C# Returns Zero Results将 AdvancedSearch for Outlook 与 C# 一起使用返回零结果
【发布时间】:2016-11-29 13:23:03
【问题描述】:

我正在尝试在我的收件箱和所有子文件夹中搜索主题行中的给定字符串。我在网上找到了以下代码(https://www.add-in-express.com/creating-addins-blog/2012/05/31/outlook-search-csharp-vbnet/),但它返回的结果为零,这不是预期的结果。

我查看了 Outlook 中视图设置下的过滤器,以查找在 Outlook Explorer 中返回结果的给定搜索词,并得到以下查询:“http://schemas.microsoft.com/mapi/proptag/0x0037001f”LIKE '%Ticket%' 当我将其插入以下代码时,我同样得到零结果。

当我使用 LINQ 查询这些文件夹时(LINQ 太慢,无法在这里成为真正的解决方案),我可以得到结果,所以我猜我在使用高级搜索时会出现语法错误。很难在网络上找到使用示例。我会感谢任何可以帮助我的人。

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

【问题讨论】:

    标签: c# search outlook advanced-search


    【解决方案1】:

    我等待结果的时间不够长。当 AdvancedSearch(在单独的线程中运行)完成时,它会触发一个名为 AdvancedSearchComplete 的事件。为了等待搜索完成,我必须告诉代码处理事件。

    在 RunAdvancedSearch 中,我在 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");
            }
            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;
        }
        //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?");
        }
    

    【讨论】:

    • 我刚刚移动了您的return 声明。我想这就是你想要的地方?
    【解决方案2】:

    您的过滤器运行良好,请使用应用程序

    private Search RunAdvancedSearch(Outlook.Application OutlookApp, string wordInSubject)
    

    https://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.outlook.application.aspx

    在 msdn“备注”中阅读有关使用 _Application 和 Application 的信息。写的很好。

    【讨论】:

    • 我将 ._Application 更改为 .Application,但结果相同 - 零。您或许还有其他想法?
    猜你喜欢
    • 2023-02-15
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2010-12-26
    相关资源
    最近更新 更多