patriciaxxx
Programmer
I have the following two functions. The first one I can parse parameter as follows:-
IsFileOpen("C:\test.doc")
My question is how do I parse multiple files for the second function (arrays still baffle me a bit):-
CheckForOpenFiles()
IsFileOpen("C:\test.doc")
My question is how do I parse multiple files for the second function (arrays still baffle me a bit):-
CheckForOpenFiles()
Code:
[COLOR=#204A87]Public Function IsFileOpen(PathName As String) As Boolean
On Error GoTo ErrHandler
Dim i As Integer
If Len(Dir$(PathName)) Then
i = FreeFile()
Open PathName For Random Access Read Write Lock Read Write As #i
Lock i 'Redundant but let's be 100% sure.
Unlock i
Close i
Else
Err.Raise 53
End If
ExitProc:
On Error GoTo 0
Exit Function
ErrHandler:
Select Case Err.Number
Case 70 'Unable to acquire exclusive lock.
IsFileOpen = True
Case Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
End Select
Resume ExitProc
Resume
End Function
Public Function CheckForOpenFiles(Files() As String) As Boolean
On Error GoTo ErrHandler
Dim i As Long
Dim lngLocks As Long
Dim strFiles() As String
Dim strMessage As String
Do
lngLocks = 0
For i = 0 To UBound(Files)
If IsFileOpen(Files(i)) Then
ReDim Preserve strFiles(lngLocks)
strFiles(lngLocks) = Files(i)
lngLocks = lngLocks + 1
End If
Next
If lngLocks Then
strMessage = "The following files are in use. " & _
"Please close the application that may have it open." _
& vbNewLine & vbNewLine
For i = 0 To UBound(strFiles)
strMessage = strMessage & strFiles(i) & vbNewLine
Next
If vbCancel = MsgBox(strMessage, vbRetryCancel, "Files in use") Then
CheckForOpenFiles = False
Exit Do
End If
End If
Loop Until lngLocks = 0
If lngLocks = 0 Then
CheckForOpenFiles = True
End If
ExitProc:
On Error GoTo 0
Exit Function
ErrHandler:
Select Case Err.Number
Case 53 'File doesn't exist, ignore.
Resume Next
Case Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
End Select
Resume ExitProc
Resume
End Function
[/color]