Microsoft KB Archive/269176

= OL97: How to Programmatically Search a Folder Tree =

Article ID: 269176

Article Last Modified on 1/27/2007

-

APPLIES TO


 * Microsoft Outlook 97 Standard Edition

-



This article was previously published under Q269176





SUMMARY
This article provides a code sample that shows how to programmatically search for items that contain a specific string in the Subject field. This procedure searches a selected folder and all of its subfolders.



MORE INFORMATION
The following sample is designed for use in Microsoft Visual Basic or in Visual Basic for Applications in another Microsoft Office program. Verify that you reference the Microsoft Outlook 8.0 Object Library. The code sample in this section begins to search the folder that is currently selected. For additional information about how to reference a different folder, click the article number below to view the article in the Microsoft Knowledge Base:

180696 OL97: Programming Examples for Referencing Items and Folders

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

Dim strSearchString As String Dim lCountOfFound As Long

Sub WalkFolders

Dim olApp As Outlook.Application Dim olSession As Outlook.NameSpace Dim olStartFolder As Outlook.MAPIFolder Dim strPrompt As String

'Initialize count of folders searched lCountOfFound = 0

' Get a reference to the Outlook application and session. Set olApp = New Outlook.Application Set olSession = olApp.GetNameSpace(&quot;MAPI&quot;)

' Allow the user to input the search string. strPrompt = &quot;Enter the search string to be found in the subject:&quot; strSearchString = InputBox(strPrompt) If strSearchString <> &quot;&quot; Then

' Start with the currently selected folder Set olStartFolder = olApp.ActiveExplorer.CurrentFolder

' Start the search process. ProcessFolder olStartFolder MsgBox CStr(lCountOfFound) & &quot; messages were found.&quot; End If

End Sub

Sub ProcessFolder(CurrentFolder As Outlook.MAPIFolder)

Dim i As Long Dim olNewFolder As Outlook.MAPIFolder ' late bind this object variable, since it could be various item types Dim olTempItem As Object

' Loop through the items in the current folder. ' Looping through backwards in case items are to be deleted, ' as this is the proper way to delete items in a collection. For i = CurrentFolder.Items.Count To 1 Step -1

Set olTempItem = CurrentFolder.Items(i)

' Check to see if a match is found If InStr(1, olTempItem.Subject, strSearchString, 0) > 0 Then

' The following are examples of what you can do: ' 1. To notify that message was found: ' MsgBox &quot;Found message with subject: &quot; & olTempItem.Subject '        ' 2. To delete the item: ' olTempItem.Delete '        ' 3. To move the item: '   NOTE: You need to first define olDestFolder ' olTempItem.Move olDestFolder '        lCountOfFound = lCountOfFound + 1

End If

Next

' Loop through and search each subfolder of the current folder. For Each olNewFolder In CurrentFolder.Folders

If olNewFolder.Name <> &quot;Deleted Items&quot; Then ProcessFolder olNewFolder End If

Next

End Sub

