Article ID: 184982
Article Last Modified on 1/22/2007
Sub YourMacro()
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 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
FileLocked = True
Err.Clear
End If
End Function
For additional information, please see the following articles in the
Microsoft Knowledge Base:
173707 OFF97: How to Run Sample Code from Knowledge Base Articles
159345 WD97: Macro to Determine Whether File Is Already Open
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: wordcon vb vba vbe
Keywords: kbdtacode kbhowto kbprogramming KB184982