Opening files through code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

TIA:

Trying to write code to open all files (one at a time) within specified folder

something like:
for each file with in folder c:\my documents
open
do something
save
close
next

Thanks, Joel
 
Hi Joel

This shoud do the trick for you.

Sub OpenChange()

Dim w As Workbook

With Application.FileSearch
.NewSearch
.LookIn = "C:\My Documents"
.SearchSubFolders = False
.Filename = ".xls"
.FileType = msoFileTypeAllFiles
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
Workbooks.Open (.FoundFiles(i))
'Do stuff here.
For Each w In Workbooks
If w.Name <> ThisWorkbook.Name Then
w.Close savechanges:=True
End If
Next w

Next i
Else
MsgBox "There were no files found."
End If
End With

End Sub

Regards

Marcus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top