Visual Basic Programming Code Examples Visual Basic > Files Directories Drives Code Examples Determine if a File is open Determine if a File is open The following routine checks to see if a file has been opened/locked: Option Explicit 'Purpose : This function checks to see if a file is open or not 'Inputs : sFilePathName The file and path name of the file eg. C:\book1.xls 'Outputs : Returns True if the file is open else returns False 'Notes : This will return False if the file has not been open exclusively. ' eg. Opening a text file in a text editor will not lock the file and hence ' this routine will still return False. 'Revisions : Function FileIsOpen(sFileName As String) As Boolean Dim iFileNum As Integer, lErrNum As Long On Error Resume Next iFileNum = FreeFile() 'Attempt to open the file and lock it. Open sFileName For Input Lock Read As #iFileNum Close iFileNum lErrNum = Err.Number On Error GoTo 0 'Check to see which error occurred. Select Case lErrNum Case 0 'No error occurred. 'File is NOT already open by another user. FileIsOpen = False Case 70 'Error number for "Permission Denied." 'File is already opened by another user. FileIsOpen = True Case 53 'File not found FileIsOpen = False Case Else 'Another error occurred. FileIsOpen = True Debug.Print Error(lErrNum) End Select End Function