Microsoft KB Archive/263025

= OL2000: How to Programmatically Search a Folder Tree =

Article ID: 263025

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Outlook 2000 Standard Edition

-



This article was previously published under Q263025





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 Outlook Visual Basic for Applications. To use this sample in Visual Basic or other Microsoft Office programs, verify that the Microsoft Outlook 9.0 Object Library is referenced, and then change the following line of code

Set olApp = Application to: Set olApp = CreateObject(&quot;Outlook.Application&quot;) The code sample in this section uses the PickFolder method to prompt for which folder to search, however, this method is not available in Microsoft Outlook 97. For additional information about how to reference a different folder, click the article number below to view the article in the Microsoft Knowledge Base:

208520 OL2000: 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 = 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

' Allow the user to pick the folder in which to start the search. Set olStartFolder = olSession.PickFolder

' Check to make sure user didn't cancel PickFolder dialog. If Not (olStartFolder Is Nothing) Then ' Start the search process. ProcessFolder olStartFolder MsgBox CStr(lCountOfFound) & &quot; messages were found.&quot; End If  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 For additional information about how to assign the WalkFolders macro to a toolbar button, click the article number below to view the article in the Microsoft Knowledge Base:

252426 OL2000: How to Assign a Macro to a Toolbar Button

