Function IsFileOpen(Filename As String)
'Excel:Check If a File Is Already Open
'Ease of Use :Easy
'Version tested with 2000
'Submitted by: xld
'Description:
'Simple function that checks whether a file is already open within the host application,
'returning True or 'False accordingly.
'Discussion:
'Good programming practice suggests that it is wise to check before taking certain actions.
'One such 'check is before opening a file,
'check whether it is already open or not.
'The code below provides a 'simple function that does this, returning True or False.
'There is also a very simple test program to 'demonstrate the function in use.
'It is possible to run the function from a worksheet formula
'(=IsFileOpen("C:\MyTest\volker2.xls")) although I struggle to see any valid use of this.
Dim filenum As Integer, Errnum As Integer
On Error Resume Next
filenum = FreeFile()
Open Filename For Input Lock Read As #filenum
Close filenum
Errnum = Err
On Error GoTo 0
Select Case Errnum
Case 0
IsFileOpen = False
Case 70
IsFileOpen = True
End Select
End Function