File Already Open

How to check if a file has already been opened by another user
This can be useful if you are running a batch job and want to prevent the dialog box appearing asking if you want to make a copy of the file.

Public Function FileLocked(sFileName As String) As Boolean 
   On Error Resume Next
'if the file has already been opened then the Open operation fails and an error occurs
   Open sFileName For Binary Access Read Lock Read As #1
   Close #1
   If Err.Number <> 0 Then
      FileLocked = True
      Err.Clear
   End If
End Function

The VBA alternative, while not terribly complex, does require more code because the Dir() function isn't as consistent as the FileExists property and there are more situations to consider.



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext