Skipping Read Only Files

R

Ric

Hi there, I have a macro that uses the Application.Filesearch to open lots
of files, do something to them, save them and then close them. I was
wondering how I would go about making sure each file was not read only, and
if it were not opening it/saving it/changing it. The maro works fine until
it finds a read only document then it stops.
 
P

Peter Hewett

Hi Ric

Try this code. It uses an error handler so all errors should be caught. It
also creates a log file containing the full path of each file it modifies.
Or if there is an error it contains the error number, description and full
path of the file that caused the error. You can just sear the log file for
the text "Error:".

This code needs to go in a module:

<--------------------------Module Code----------------------------------->
Private logFile As LogIt

Public Sub SetTemplate2Normal()
Const cPathToUse As String = "F:\My Templates\Test Documents\"
Const cLogFile As String = "File Update.log"

Dim docToModify As Word.Document
Dim lngIndex As Long

' Create log file
Set logFile = New LogIt
logFile.Path = cPathToUse
logFile.File = cLogFile

' Set the folder to search and type of file
With Application.FileSearch
.NewSearch
.LookIn = cPathToUse
.SearchSubFolders = False
.FileName = "*.doc"
.FileType = msoFileTypeWordDocuments
If .Execute() > 0 Then
Application.ScreenUpdating = False
For lngIndex = 1 To .FoundFiles.Count

UpdateDocument .FoundFiles(lngIndex)
Next lngIndex
Application.ScreenUpdating = True
Else
MsgBox "There are no files is the search" & _
" path that match the search mask"
End If
MsgBox .FoundFiles.Count & " files modified!"
End With
End Sub

Private Sub UpdateDocument(ByVal strFullName As String)
Dim docToModify As Word.Document

On Error GoTo UpdateDocument_Error
Set docToModify = Documents.Open(strFullName, Visible:=False)
StatusBar = "Modifying: " & strFullName

' Update the document
WordBasic.DisableAutoMacros True
With docToModify
.UpdateStylesOnOpen = False
.AttachedTemplate = NormalTemplate.Name
.Save
.Close savechanges:=wdSaveChanges
End With

logFile.Output "Updated: " & strFullName

UpdateDocument_Exit:
WordBasic.DisableAutoMacros False
Exit Sub

UpdateDocument_Error:
logFile.Output "Error: (" & Err.Number & ") " & _
Err.Description & ", Updating: " & strFullName
On Error Resume Next
docToModify.Close wdDoNotSaveChanges
On Error GoTo 0
Resume UpdateDocument_Exit
End Sub

<-------------------------End of Module Code----------------------------->

This code MUST go in a class module named "Logit":
<----------------------------Class Code---------------------------------->

Private mstrFile As String
Private mstrPath As String
Private mboolEnabled As Boolean
Private mboolTimeStamp As Boolean

Public Sub Output(ByVal strText As String)
Const cProcedureName As String = "Log::Output"

Dim hFile As Long

On Error GoTo OutputError

' Everything must be right for us to log the passed text
If CanOutput Then

' Prefix log text with timestamp
If TimeStamp Then strText = Now & vbTab & strText

' Open log file for append
hFile = FreeFile
Open mstrPath & mstrFile For Append Access Write As hFile

' Write the output and tidy up
Print #hFile, strText
Close hFile
End If

OutputExit:
Exit Sub

OutputError:
mboolEnabled = False
Err.Raise Err.Number, cProcedureName, Err.Description
End Sub ' Output

Public Sub Kill()
On Error GoTo HandleError

' Delete the current log file if it exists
If Len(Dir$(mstrPath & mstrFile)) > 0 Then
Kill mstrPath & mstrFile
End If

ExitHere:
Exit Sub

HandleError:
mboolEnabled = False
Err.Raise vbObjectError + 8001, "Log::Kill", _
Err.Description
Resume ExitHere
End Sub ' Reset

Public Property Get Path() As String
Path = mstrPath
End Property
Public Property Let Path(ByVal strPath As String)
strPath = Trim$(strPath)
If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
mstrPath = Trim$(strPath)
End Property

Public Property Get File() As String
File = mstrFile
End Property
Public Property Let File(ByVal FileName As String)
mstrFile = FileName
End Property

Public Property Get Enabled() As Boolean
Enabled = mboolEnabled
End Property
Public Property Let Enabled(ByVal EnableLogFile As Boolean)
mboolEnabled = EnableLogFile
End Property

Public Property Get TimeStamp() As Boolean
TimeStamp = mboolTimeStamp
End Property
Public Property Let TimeStamp(ByVal TimeStampOutput As Boolean)
mboolTimeStamp = TimeStampOutput
End Property

Public Property Get CanOutput() As Boolean
CanOutput = LenB(Path) > 0 And LenB(File) > 0 And Enabled
End Property


Private Sub Class_Initialize()
Me.Enabled = True
End Sub
<-------------------------End of Class Code------------------------------>


HTH + Cheers - Peter
 

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

Top