按 task.body 文本搜索用户任务

Searching user tasks by task.body text

大家早上好!我花了很多时间搜索和阅读有关此的论坛,但我似乎找不到解决方案。非常感谢你们能提供的任何帮助。

我已经通过 C# 和 Visual Studio 2013 创建了一个 Outlook Add-In。这个 add-in 将创建一些自定义任务,这是有效的。我遇到的问题是搜索那些自定义创建的任务并删除它们。我有代码可以根据严格的主题行进行搜索,但没有使用 "LIKE" 来查找部分搜索匹配项。我还读到 find 方法无法搜索 body,因此高级搜索更好。我正在尝试使用我能找到的高级搜索文档,但我不确定 "scope" 参数对于任务来说是什么,或者即使这是最好的解决方案。

我认为最好的方法是将所有自定义创建的任务附加到标记为 "DO NOT DELETE" 的页脚并在此处输出文本,我可以搜索以确定该任务是由我的 add-in 创建的,然后删除它。我还考虑过在创建自定义任务时存储自定义任务的 EntryID,但我了解到这个数字可以更改,所以我不确定这是否是始终查找和删除自定义创建任务的最佳方法。

我希望你们中的任何一个人能够协助编写一个代码示例,该示例在用户的任务文件夹中搜索任务 body 中包含字符串的所有任务。或者,我绝对可以搜索主题行中包含字符串的所有任务。对此我很紧张,非常感谢你们提供的任何指导、文章或代码示例!我要么在 VB 中找到示例,要么找到我无法付诸实践的部分解释。

编辑:已解决

感谢以下标记为答案的回复,我想在这里提供更多代码细节,以防将来body需要更详细的答案。此解决方案不会搜索此 post 标题中所述的 body 文本,因为该解决方案不是完成我所需内容的最佳方式。

正在创建任务

using Outlook = Microsoft.Office.Interop.Outlook;

Outlook.Application app = new Outlook.Application();
Outlook.TaskItem task = app.CreateItem(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem;
Outlook.UserProperties taskUserProperties = null;
Outlook.UserProperty taskUserProperty = null;

try {
    taskUserProperties = task.UserProperties;
    taskUserProperty = taskUserProperties.Add("Custom Property Name", Outlook.OlUserPropertyType.olText, true, 1);
    taskUserProperty.Value = "Custom value";
    task.Save();
}
catch (Exception ex) {
    MessageBox.Show(ex.Message);
}
finally {
    if (taskUserProperty != null) Marshal.ReleaseComObject(taskUserProperty);
    if (taskUserProperties != null) Marshal.ReleaseComObject(taskUserProperties);
}

找到它

string searchCriteria = "[Custom Property Name] = 'VALUE TO FIND'";
Outlook.Application app = new Outlook.Application();
Outlook._NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder folder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
Outlook._TaskItem taskItem = null;
Outlook.Items folderItems = null;
object resultItem = null;

try {
    folderItems = folder.Items;
    folderItems.IncludeRecurrences = true;

    if (folderItems.Count > 0) {
        resultItem = folderItems.Find(searchCriteria);

        if (resultItem != null) {
            if (resultItem is Outlook._TaskItem) {
                taskItem = resultItem as Outlook._TaskItem;

                MessageBox.Show(taskItem.Subject, "Task found!", MessageBoxButtons.OK);
            }
        }

        Marshal.ReleaseComObject(resultItem);
    }
}
catch (Exception ex) {
    MessageBox.Show(ex.Message);
}
finally {
    if (folderItems != null) Marshal.ReleaseComObject(folderItems);
}

为什么不使用用户属性 (TaksItem.UserPropertiers.Add)? 如果将用户字段添加到文件夹字段,您可以使用 Items.Find/FindNext/Restrict.

搜索 属性