Showing posts with label Excel VBA NewSearch method. Show all posts
Showing posts with label Excel VBA NewSearch method. Show all posts

Monday, June 30, 2008

Search and Open Files using Excel VBA (FileSearch)

Sort Files By Date using Excel VBA File Search

Use the FileSearch property to return the FileSearch object. The following example searches for files and displays the number of files found and the name of each file.



Sub App_FileSearch_Example()

Dim OWB As Workbook
With Application.FileSearch
.NewSearch
.LookIn = "c:\vbadud\delivered_files\"
.FileName = "*.xls"
If .Execute(SortBy:=msoSortByLastModified, SortOrder:=msoSortOrderDescending) > 0 Then

For i1 = 1 To .FoundFiles.Count
Set OWB = Workbooks.Open(.FoundFiles(i1))
OWB.Save
OWB.Close
Next i1

End If

End With

End Sub

Use the NewSearch method to reset the search criteria to the default settings. All property values are retained after each search is run, and by using the NewSearch method you can selectively set properties for the next file search without manually resetting previous property values. The following example resets the search criteria to the default settings before beginning a new search.

The above example will search for all excel files in a particular folder and opens the latest file. This method can also be used to Identify the last-modified or latest file using Excel VBA.

See also:

Open Excel Files - Open Dialog - GetOpenFilename Method

Browse a Folder / Select a Folder Thru Shell

OpenFileDialog in Visual Basic .Net

Visual Basic Common Dialog

Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.