Outlook - 搜索例程仅在调试模式下有效
Outlook - Search Routine Only Works In Debug Mode
我在 C# 中有一个例程,通过过滤联系人文件夹在 Outlook 中搜索联系人,当我逐步调试它时它可以工作,但是如果我让它 运行 单独(没有调试中断)然后它什么都不做,Outlook返回的结果只是空数组,没有异常,什么都没有。这是套路:
private List<Outlook.ContactItem> filterContactFolder(String searchStr, Outlook.Folder folder)
{
List<Outlook.ContactItem> contacts = new List<Outlook.ContactItem>();
string filter = "urn:schemas:contacts:fileas LIKE '%" + searchStr + "%'";
Outlook.Search searchObject = null;
String scope = String.Empty;
try
{
scope = "'" + folder.FolderPath + "'";
searchObject = Globals.ThisAddIn.Application.AdvancedSearch(scope, filter, false, Type.Missing);
foreach (Outlook.ContactItem c in searchObject.Results)
{
contacts.Add(c);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
if (searchObject != null) searchObject.ReleaseComObject();
}
return contacts;
}
有人知道发生了什么事吗?
您尝试过早获得结果。
关键事实是搜索是在另一个线程中执行的。您需要处理应用程序 class 的 AdvancedSearchComplete 事件,该事件在 AdvancedSearch 方法完成时触发。
您可能会发现 Advanced search in Outlook programmatically: C#, VB.NET 文章有帮助。
我在 C# 中有一个例程,通过过滤联系人文件夹在 Outlook 中搜索联系人,当我逐步调试它时它可以工作,但是如果我让它 运行 单独(没有调试中断)然后它什么都不做,Outlook返回的结果只是空数组,没有异常,什么都没有。这是套路:
private List<Outlook.ContactItem> filterContactFolder(String searchStr, Outlook.Folder folder)
{
List<Outlook.ContactItem> contacts = new List<Outlook.ContactItem>();
string filter = "urn:schemas:contacts:fileas LIKE '%" + searchStr + "%'";
Outlook.Search searchObject = null;
String scope = String.Empty;
try
{
scope = "'" + folder.FolderPath + "'";
searchObject = Globals.ThisAddIn.Application.AdvancedSearch(scope, filter, false, Type.Missing);
foreach (Outlook.ContactItem c in searchObject.Results)
{
contacts.Add(c);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
if (searchObject != null) searchObject.ReleaseComObject();
}
return contacts;
}
有人知道发生了什么事吗?
您尝试过早获得结果。
关键事实是搜索是在另一个线程中执行的。您需要处理应用程序 class 的 AdvancedSearchComplete 事件,该事件在 AdvancedSearch 方法完成时触发。
您可能会发现 Advanced search in Outlook programmatically: C#, VB.NET 文章有帮助。