Поделиться через


How to: Search Within a Specific Folder

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.

Applies to: The information in this topic applies to application-level projects for Outlook 2007 and Outlook 2010. For more information, see Features Available by Office Application and Project Type.

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
        MsgBox(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

Other Resources

Outlook Object Model Overview