'The following sample Visual Basic for Applications macro calls the function FileLocked and
'passes the full path and name of the file for testing. If the function returns True, error
'number 70 "Permission Denied" has most likely occurred, and the file is currently open and
'locked by another process. If the function returns False, the file is not open, and the
'macro opens the document.
Sub IsFileLocked()
Dim strFileName As String
' Full path and name of file.
strFileName = "C:\test.doc"
' Call function to test file lock.
If Not FileLocked(strFileName) Then
' If the function returns False, open the document.
Documents.Open strFileName
End If
End Sub
Function FileLocked(strFileName As String) As Boolean
On Error Resume Next
' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
Open strFileName For Binary Access Read Lock Read As #1
Close #1
' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
' Display the error number and description.
MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description
FileLocked = True
Err.Clear
End If
End Function
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.