26.7. Searching for Items

To search for items, use the AdvancedSearch method of the Application object. The syntax is as follows:

expression .AdvancedSearch(Scope, Filter, SearchSubFolders, Tag)

Here are the components of the syntax:

  • expression is a required expression that returns an Application object.

  • Scope is a required String argument that specifies the scope of the search (which items to search). Usually you'll search a particular folder. For example, you might search the Inbox for messages that match certain criteria, or you might search the Tasks folder for particular tasks.

  • Filter is an optional Variant argument that specifies the search filter. While this argument is optional, you will need to use it unless you want to return all the items within the scope you've specified.

  • SearchSubFolders is an optional Variant argument that you can set to True to search through any subfolders of the folder specified by the Scope argument or False to search only the specified folder. The default is False.

  • Tag is an optional Variant argument that you can use to specify a name for the search you're defining. If you create a name, you can call the search again.

What's DASL?

DASL is the abbreviation for DAV Search and Locating, a protocol attempting to achieve status. DAV is the acronym for Distributed Authoring and Versioning. For more details, see http://www.webdav.org/dasl/.


The complex part of performing an advanced search is creating the search filter, which needs to be in DASL format. But you can have Outlook put together the filter for you. To do so, first put the Filter button on a toolbar:

  1. In Outlook, choose Tools Customize to display the Customize dialog box. Click the Commands tab if it isn't already displayed.

  2. In the Categories list box, click the View item.

  3. In the Command list box, scroll down to the Filter command, and then drag it to a toolbar. (If you prefer, you can put the Filter command on a menu rather than on a toolbar. Enjoy menus and toolbars while you can; Outlook is one of the few Vista applications that still offers these now-historical tools. The Ribbon has arrived.)

  4. Click the Close button to close the Customize dialog box.

Now you can display the Filter dialog box and put the filter together:

  1. Click the Filter button on the toolbar to display the Filter dialog box (see Figure 26.4).

  2. Use the controls on the Messages, More Choices, and Advanced pages to specify the filter you want.

    Figure 26.4. Use the Messages, More Choices, and Advanced pages of the Filter dialog box to put together the DASL filter needed for an advanced search.
  3. Click the SQL tab to display the SQL page, which contains the filter that you've created.

  4. Select the Edit These Criteria Directly. All Other Tabs Will Be Unavailable checkbox to make the Find Items That Meet These Criteria text box available.

  5. Drag with the mouse to select the filter, and then press Ctrl+C to copy it to the Clipboard.

  6. Click the Cancel button to close the Filter dialog box.

  7. Press Alt+F11 to switch to the Visual Basic Editor, and then paste the filter into your code.

The following example searches the Inbox (Scope: = "Inbox") for messages with the subject line Dam Project (Filter: = "urn:schemas:mailheader:subject = 'Dam Project'"). If any messages are found, the procedure produces a list of sender names, which it assigns to the String variable strMessages. If no messages are found, the procedure assigns to strMessages text to let the user know this. The procedure then displays strMessages in a message box with the caption Search Results.

Sub Sample_Advanced_Search()

    Dim mySearch As Search
    Dim myResults As Results
    Dim intCounter As Integer

Dim strMessages As String

    Set mySearch = AdvancedSearch(Scope:="Inbox", _
        Filter:="urn:schemas:mailheader:subject = 'Dam Project'")
    Set myResults = mySearch.Results
    If myResults.Count > 0 Then
        strMessages = "The Inbox contains messages that match " & _
            "the search criteria from these senders:" & vbCr & vbCr
    For intCounter = 1 To myResults.Count
        strMessages = strMessages & _
            myResults.Item(intCounter).SenderName & vbCr
    Next intCounter
    Else
        strMessages = "The Inbox contains no messages " & _
            "that match the search criteria."
    End If
    MsgBox strMessages, vbOKOnly, "Search Results"
End Sub

You Can Execute 100 Searches Simultaneously, But Should You?

If necessary, you can run two or more searches at the same time. To do so, use the AdvancedSearch method in successive lines of code. Actually, you can run up to 100 searches at the same time, but doing so puts a considerable load on your computer and may make it run slowly or appear to stop responding.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.195.251