Export (0) Print
Expand All
Expand Minimize

Using the SearchFolders Collection

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
 

Paul Cornell
Microsoft Corporation

March 2001

Applies to:
   Microsoft® Office XP

Summary: Describes how to programmatically search for files by using the SearchFolders collection, a member of the Microsoft Office 10.0 object library. (4 printed pages)

Download ODC_SrchFldrsCol.exe.


Contents

Introduction Exploring the Object Model for Building Search Criteria Example: Searching Folders for Files Containing Specific Text Conclusion

Introduction

The FileSearch object in the Microsoft® Office 9.0 type library allows you to programmatically search for files matching criteria such as an author’s name, the type of file, the date that the file was last modified, and so on. Additionally, you can narrow the search criteria to known folders, and optionally, any of their subfolders. However, there is no method to search for folders if you don’t know their paths.

The addition of the SearchFolders collection to the FileSearch object in the Office 10.0 type library now allows you to programmatically search for files in multiple folders, even if you don’t know their paths. This article explores the new SearchFolders collection and provides an example of how to use this new collection by using Visual Basic® for Applications (VBA) code in Office XP.

Exploring the Object Model for Building Search Criteria

To better understand how to programmatically build search criteria, Figure 1 below shows the various collections and objects that can be used, and Table 1 below describes the purpose of the SearchFolders collection and its child objects.

Aa164017.odc_srchfldrscol1(en-us,office.10).gif

Figure 1. The FileSearch object model

Table 1. The SearchFolders collection and its child objects

Object/collectionPurpose
SearchFoldersRepresents a collection of folders to be searched by using the FileSearch object. There is only one SearchFolders collection per application. You add one or more ScopeFolder objects to this single SearchFolders collection.
ScopeFolderRepresents a folder or subfolder that you want to add to the search. Each ScopeFolder object may contain zero or more ScopeFolders objects (which are collections of subfolders). If a ScopeFolder object does not have an accompanying ScopeFolders collection of child ScopeFolder objects, this means that the folder has no subfolders (however, the folder may contain files).
ScopeFoldersRepresents a collection of one or more subfolders that belong to a parent ScopeFolder object.

Other Search Objects

Some of the other collections and objects in the FileSearch object model (from Figure 1 above) that deserve brief mention are:

  • FileTypes collection (new in Office XP)   This collection represents one or more file types that can be searched (for instance, Microsoft Excel files or Microsoft PowerPoint® files). There is only one FileTypes collection in an application. Calling the NewSearch method of the FileSearch object clears any previous settings in the FileTypes collection (for example, Application.FileSearch.NewSearch). This is equivalent to the choices presented in the Results should be box in the Basic Search task pane (accessed by clicking Search on the File menu in all Office applications, except Microsoft Outlook®).
  • FoundFiles collection   This collection represents zero or more files returned from a file search. You can use a For Each…Next loop together with the FoundFiles collection to take some sort of action on each file that you find, such as opening or printing it. This is equivalent to the list of files presented in the Search Results task pane accessed by clicking Search from the File menu. Calling the Execute method of the FileSearch object begins the file search and updates the FoundFiles collection (for example, Application.FileSearch.Execute).
  • PropertyTests collection   This collection represents one or more advanced search criteria to be applied to a basic search. This is equivalent to the choices presented in the Search for area of the Advanced Search task pane (accessed by clicking Advanced Search on the Basic Search task pane).
  • SearchScopes collection (new in Office XP)   This collection represents the available local or networked computer resources that you can search (for example, My Computer, My Network Places, or Microsoft Outlook). You can't add or remove SearchScope objects from the SearchScopes collection. This is equivalent to the choices presented in the Search in box of the Search task pane.

For more information about the methods and properties of the above collections, see Microsoft Office XP VBA Help.

Example: Searching Folders for Files Containing Specific Text

The example that accompanies this article, available for download, demonstrates how the SearchFolders collection works. In the example, when the user selects some text within a Microsoft Word document and provides a folder name through a custom UserForm, the code searches the user’s local computer for any files that contain the selected text in any of the folders that match the folder name provided by the user. The code then uses the custom UserForm to report the list of matching files to the user.

Before your code searches for any files, it should clear any previous search criteria. The custom ClearPreviousSearchFolders subroutine does this by removing any remaining SearchFolder objects from the single SearchFolders collection in the application. In addition, this subroutine sets the LookIn property to a blank string and calls the NewSearch method to reset all of the remaining search criteria to their default settings.

To search through all of the folders on a user’s computer, the custom, recursive SearchForSubFolders subroutine is used. Without this subroutine, the search criteria would be limited to only the first level of folders on the user’s computer. The SearchForSubFolders subroutine calls itself as many times as needed to traverse through the computer’s entire directory structure. Each time the subroutine finds a folder name that matches the folder name provided by the user, it calls the AddToSearchFolders method to add that folder to the single SearchFolders collection for the application.

After the entire directory structure of the user’s computer has been traversed, the code then is ready to search through the SearchFolders collection for any files containing the user’s selected text. The Execute method of the FileSearch object, in conjunction with the LookIn property and the Add method of the PropertyTests collection, returns a FoundFiles collection that contains the matching files, which are reported to the user by calling the custom ReportResultsToUser subroutine to update and display the custom UserForm.

Conclusion

The SearchFolders collection allows you to programmatically search for files in multiple folders, even if you don’t know their paths beforehand. And because the SearchFolders collection is part of the FileSearch object, you can easily incorporate additional search criteria into your programmatic searching solutions.

Show:
© 2014 Microsoft