Udostępnij za pośrednictwem


How to: Search Within a Specific Folder

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

Microsoft Office version

  • Outlook 2003

  • Outlook 2007

For more information, see Features Available by Application and Project Type.

This code example uses the Find and FindNext methods to search for text in the subject field of e-mail messages that are in the Inbox. This method uses a string filter to check for the letter T as the starting letter of the Subject text.

Example

Private Sub SearchInBox()
    Dim inbox As Outlook.MAPIFolder = Me.Application.ActiveExplorer().Session. _
        GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
    Dim items As Outlook.Items = inbox.Items
    Dim mailItem As Outlook.MailItem = Nothing 
    Dim folderItem As Object 
    Dim subjectName As String = String.Empty
    Dim filter As String = "[Subject] > 's' And [Subject] <'u'"
    folderItem = items.Find(filter)
    While (folderItem IsNot Nothing)
        mailItem = TryCast(folderItem, Outlook.MailItem)
        If mailItem IsNot Nothing Then
            subjectName = vbCrLf & mailItem.Subject
        End If
        folderItem = items.FindNext()
    End While
    subjectName = "The following e-mail messages were found: " _
        & subjectName
    MessageBox.Show(subjectName)
End Sub
private void SearchInBox()
{
    Outlook.MAPIFolder inbox = this.Application.ActiveExplorer().Session.
        GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
    Outlook.Items items = inbox.Items;
    Outlook.MailItem mailItem = null;
    object folderItem;
    string subjectName = string.Empty;
    string filter = "[Subject] > 's' And [Subject] <'u'";
    folderItem = items.Find(filter);
    while (folderItem != null)
    {
        mailItem = folderItem as Outlook.MailItem;
        if (mailItem != null)
        {
            subjectName += "\n" + mailItem.Subject;
        }
        folderItem = items.FindNext();
    }
    subjectName = " The following e-mail messages were found: " +
        subjectName;
    MessageBox.Show(subjectName);
}

See Also

Tasks

How to: Retrieve a Folder by Name

Concepts

Working with Folders

Outlook Object Model Overview