Option Explicit
Sub OpenDocument()
Dim sFileName As String
' \\ Full path of test file
sFileName = ThisDocument.Path & "\Test.doc"
' \\ Only open File if the file is not opened yet
If Not IsFileLocked(sFileName) Then
Application.Documents.Open FileName:=sFileName
End If
End Sub
' \\ This function checks if the file is allready opened by another process,
' \\ and if the specified type of access is not allowed.
' \\ If so the Open method will fail and a error occurs!
Function IsFileLocked(sFile As String) As Boolean
On Error Resume Next
' \\ Open the file
Open sFile For Binary Access Read Write Lock Read Write As #1
' \\ Close the file
Close #1
' \\ If error occurs the document if open!
If Err.Number <> 0 Then
' \\ msgbox for demonstration purposes
MsgBox Err.Description, vbExclamation, "Warning File is opened"
'\\ Return true and clear error
IsFileLocked = True
Err.Clear
End If
End Function
|