505.369.1650 [email protected]

Use this VBA function to determine if a file is already open.  For example, before attempting to import an Excel file into a Microsoft Access database, you can call this function, and if the file is open, notify the user that they need to close the Excel file first.  You can also use it in automation logic to decide whether or not you need to open a file before continuing, or refer to the already open file.

Public Function FileInUse(FileName As String) As Boolean
' This procedure determines if the file is open.  If it is, then
' it passes back True.
'
' FileInUse() Version 1.0.0
' Copyright © 2023 Extra Mile Data, www.extramiledata.com.
' For questions or issues, please contact [email protected].
' Use (at your own risk) and modify freely as long as proper credit is given.
On Error GoTo Err_Handler

    Open FileName For Binary Access Read Lock Read As #1
    Close #1
    
    FileInUse = False

Exit_Proc:
    On Error Resume Next
    Exit Function

Err_Handler:
    FileInUse = True
    Resume Exit_Proc
End Function