How do you check if a file is already open and locked for editing before going to open it again via VBA code in Access 2003, opening a word document?
Printable View
How do you check if a file is already open and locked for editing before going to open it again via VBA code in Access 2003, opening a word document?
Code:Sub CheckForFileStatus()
'Will return true if file is open
MsgBox IsFileOpen("P:\MyFile.Doc")
End Sub
Function IsFileOpen(FileName As String)
Dim iFilenum As Long
Dim iErr As Long
On Error Resume Next
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0
Select Case iErr
Case 0: IsFileOpen = False
Case 70: IsFileOpen = True
Case Else: Error iErr
End Select
End Function
I would add one more error number to cover the file being checked not existing. If that is the case a run time error occurs.Code:Select Case iErr
Case 0: IsFileOpen = False
Case 53: IsFileOpen = False '53 = File Not Found
Case 70: IsFileOpen = True
Case Else: Error iErr
End Select