PDA

View Full Version : "locked for editing" check



linkerschuh
10-22-2014, 11:58 PM
Hi all,

I have an access database, which opens word's and ppt's and populates certain fields in it. If several users use the database and generate at the very same moment the .doc's and the ppt's, they get a "locked-for-editing" pop-up.
What I would like to do in order to solve this, is create a loop in the VBA code which checks whether the doc is locked for editing. If it is not locked, the vba code continues, if it is locked, the check repeats itself.

Unfortunately, I don't know the codeline how to check whether a document is locked or not.

Can anybody help?

Thanks a lot!

ranman256
10-23-2014, 05:42 AM
Sub TestFile()
If IsFileLocked( sFile) Then
MsgBox "LOCKED"
Else
MsgBox "available"
End If
End Sub

Public Function IsFileLocked(psFileName 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 psFileName For Binary Access Read Write Lock Read Write 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
IsFileLocked = True
Err.Clear
End If
End Function