背景:
用Microsoft.Office.Interop.Outlook取得日歷項,然后根據業務要求篩選,
現象:
items.Restrict方法中的篩選器,使用like進行模糊查詢時,會出COMException例外,
代碼:
1 //folder取得前略 2 3 Outlook.Items items = folder.Items; 4 string rstFilter = ""; 5 try 6 { 7 rstFilter = string.Format("[Subject] like '%{0}%'", "會議"); 8 items = items.Restrict(rstFilter);//此處報例外 9 } 10 catch (Exception ex) 11 { 12 MessageBox.Show(this 13 , ex.Message + Environment.NewLine + rstFilter 14 , this.Name 15 , MessageBoxButtons.OK 16 , MessageBoxIcon.Error); 17 return; 18 }
例外:

原因:
將屬性名括在方括號內的寫法叫做“Jet 篩選器”,在 Jet 查詢中,只能對關鍵字屬性進行短語匹配,
不能通過 Jet 查詢進行開頭或子字串匹配, 即不支持like模糊查詢,
為了克服使用 Jet 查詢語法時關鍵字限制條件的局限性,請使用允許開頭或子字串限制條件的 DASL 語法,
解決:
使用與 Keywords 屬性的比較篩選專案
將篩選器字串改為如下:
rstFilter = string.Format("@SQL=http://schemas.microsoft.com/mapi/proptag/0x0037001f ci_phrasematch '{0}' ", "會議");
詳情:
Docs / Office VBA 參考 / Outlook / 操作說明主題 / 搜索和篩選 / 篩選 / 概述
其中使用DASL語法時,命名空間相關介紹:
Docs / Office VBA 參考 / Outlook / 操作說明主題 / 導航 / 屬性概述 / 按命名空間參考屬性
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/33173.html
標籤:C#
下一篇:WinDbg排查CPU高的問題
