PDA

View Full Version : find & open a file



lior03
11-30-2005, 02:37 AM
hello
the enclose macro find file in a folder.
how do i make it open the files?
Sub findafile()
Dim i As Integer
Dim x As String
x = InputBox("select a file", "locate files")
With Application.FileSearch
.NewSearch
.LookIn = "C:\Documents and Settings\user1\My Documents"
.FileType = msoFileTypeExcelWorkbooks
.FileName = x
.MatchAllWordForms = True
If .Execute > 0 Then
MsgBox "There were " & .FoundFiles.count & _
" file(s) found."
For i = 1 To .FoundFiles.count
MsgBox .FoundFiles(i), 32
Next i
Else
MsgBox "There were no files found."
End If
End With
End Sub

thanks

johnske
11-30-2005, 03:28 AM
Without testing...Sub findafile()
Dim i As Integer
Dim x As String
x = InputBox("select a file", "locate files")
With Application.FileSearch
.NewSearch
.LookIn = "C:\Documents and Settings\user1\My Documents"
.FileType = msoFileTypeExcelWorkbooks
.Filename = x
.MatchAllWordForms = True
If .Execute > 0 Then
'------------------------------------------------
For i = 1 To .FoundFiles.Count
On Error Resume Next '< already open = error
If .FoundFiles(i) <> ThisWorkbook.FullName Then
Workbooks.Open (.FoundFiles(i))
End If
Next i

'now do what you want to do
'with each open workbook
'-------------------------------------------------
Else
MsgBox "There were no files found."
End If
End With
End Sub