共用方式為


在資料夾中的項目主體中搜尋詞組

本範例會在 [收件匣] 的專案本文中搜尋字串 “office”。

範例

此程式代碼範例會使用 DAV 搜尋和尋找 (DASL) 語法來指定查詢。 為了建構篩選條件,程式代碼範例會先檢查預設存放區中是否已啟用立即搜尋,以判斷是否要針對專案本文中 「office」 的確切詞組比對使用 ci_phrasematch 關鍵詞,或使用 like 關鍵詞比對任何出現的 「office」 做為專案本文中的確切字串或子字串串。 然後,此範例會將篩選套用至收件匣上的 GetTable 方法,並在 Table 物件中取得結果。 程式代碼範例接著會顯示 Table 中每個傳回專案的主旨。

程序代碼範例會使用命名空間表示 urn:schemas:HTTPmail:textdescription 來指定 Body 屬性。

使用 ci_phrasematch 關鍵詞的語法為:

<PropertySchemaName> ci_phrasematch <ComparisonString>

使用 like 關鍵詞進行前置詞比對的語法為:

<PropertySchemaName> like <Token>%

針對任何子字串比對使用 like 關鍵詞的語法如下:

<PropertySchemaName> like %<Token>%

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and C#.

Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub DemoSearchBody()
    Dim filter As String
    If (Application.Session.DefaultStore.IsInstantSearchEnabled) Then
        filter = "@SQL=" & Chr(34) _
            & "urn:schemas:httpmail:textdescription" & Chr(34) _
            & " ci_phrasematch 'office'"
    Else
        filter = "@SQL=" & Chr(34) _
            & "urn:schemas:httpmail:textdescription" & Chr(34) _
            & " like '%office%'"
    End If
    Dim table As Outlook.Table = _
        Application.Session.GetDefaultFolder( _
        Outlook.OlDefaultFolders.olFolderInbox).GetTable( _
        filter, Outlook.OlTableContents.olUserItems)
    While Not (table.EndOfTable)
        Dim row As Outlook.Row = table.GetNextRow()
        Debug.WriteLine(row("Subject"))
    End While
End Sub
private void DemoSearchBody()
{
    string filter;
    if (Application.Session.DefaultStore.IsInstantSearchEnabled)
    {
        filter = "@SQL=" + "\""
            + "urn:schemas:httpmail:textdescription" + "\""
            + " ci_phrasematch 'office'";
    }
    else
    {
        filter = "@SQL=" + "\""
            + "urn:schemas:httpmail:textdescription" + "\""
            + " like '%office%'";
    }
    Outlook.Table table = Application.Session.GetDefaultFolder(
        Outlook.OlDefaultFolders.olFolderInbox).GetTable(
        filter, Outlook.OlTableContents.olUserItems);
    while (!table.EndOfTable)
    {
        Outlook.Row row = table.GetNextRow();
        Debug.WriteLine(row["Subject"]);
    }
}

另請參閱