ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Opening files through code (https://www.excelbanter.com/excel-programming/377278-opening-files-through-code.html)

joel

Opening files through code
 
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

joel

Opening files through code
 
Hello Everyone...should have searched first but found prior post with solution.

Joel

"Joel" wrote:

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


marcus[_3_]

Opening files through code
 
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

Joel wrote:
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




All times are GMT +1. The time now is 10:00 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com