snowman1976
Board Regular
- Joined
- Nov 4, 2008
- Messages
- 191
hello
I have the below code that I found on the site and modified it to what I want it to do, and it works great as long as all the files are closed. Basically it loops through all the files in the subfolders I have specified, does some work, closes the file and repeats. However if someone has a file open it crashes and I get an automation error. There are about 30 different files it goes through and they can get renamed without knowing, which is why I chose this method.
Does anyone have any thoughts on what I could do to just skip the file if someone else has it open? I have specified read only already but that doesnt seem to work. Note that I have stripped out the code that applies once in the file just to shorten the post down.....
Public Sub GetReported()
Dim fso, oFolder, oSubfolder, oFile, queue As Collection
Set fso = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection
queue.Add fso.GetFolder("F:\Temp\Weekend")
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Do While queue.Count > 0
Set oFolder = queue(1)
queue.Remove 1
For Each oSubfolder In oFolder.SubFolders
queue.Add oSubfolder
Next oSubfolder
For Each oFile In oFolder.Files
On Error GoTo 0
Workbooks.Open Filename:=oFile, UpdateLinks:=False, ReadOnly:=True
Dim NmStr As String
NmStr = ActiveWorkbook.Name
''does its work''
Windows(NmStr).Activate
ActiveWorkbook.Close False
Next oFile
Loop
End Sub
I have the below code that I found on the site and modified it to what I want it to do, and it works great as long as all the files are closed. Basically it loops through all the files in the subfolders I have specified, does some work, closes the file and repeats. However if someone has a file open it crashes and I get an automation error. There are about 30 different files it goes through and they can get renamed without knowing, which is why I chose this method.
Does anyone have any thoughts on what I could do to just skip the file if someone else has it open? I have specified read only already but that doesnt seem to work. Note that I have stripped out the code that applies once in the file just to shorten the post down.....
Public Sub GetReported()
Dim fso, oFolder, oSubfolder, oFile, queue As Collection
Set fso = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection
queue.Add fso.GetFolder("F:\Temp\Weekend")
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Do While queue.Count > 0
Set oFolder = queue(1)
queue.Remove 1
For Each oSubfolder In oFolder.SubFolders
queue.Add oSubfolder
Next oSubfolder
For Each oFile In oFolder.Files
On Error GoTo 0
Workbooks.Open Filename:=oFile, UpdateLinks:=False, ReadOnly:=True
Dim NmStr As String
NmStr = ActiveWorkbook.Name
''does its work''
Windows(NmStr).Activate
ActiveWorkbook.Close False
Next oFile
Loop
End Sub