Something like:
Sub Test()
Dim stDir As String
stDir = ThisWorkbook.Path
If AnyFileLocked(stDir) Then
MsgBox "File(s) open in " & stDir, vbCritical + vbOKOnly, "Error"
Exit Sub
End If
'continue...
Debug.Print "No locked files"
End Sub
Function AnyFileLocked(stDirectory As String) As Boolean
Dim sFN As Variant
If Not Right(stDirectory, 1) = "\" Then stDirectory = stDirectory & "\"
sFN = Dir(stDirectory)
While sFN <> "" And sFN <> ThisWorkbook.Name
If FileLocked(stDirectory & sFN) Then
AnyFileLocked = True
Exit Function
End If
sFN = Dir
Wend
End Function
Function FileLocked(stFileName As String) As Boolean
On Error Resume Next
Open stFileName For Binary Access Read Write Lock Read Write As #1
Close #1
If Err.Number Then
FileLocked = True
Err.Clear
Else
FileLocked = False
End If
End Function
Bookmarks