Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to see if DOC file is already open?

Microsoft Word How To?

How to see if DOC file is already open?

by  JoaoTL  Posted    (Edited  )
'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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top