Check if a file is already open

C

Cresta

Hello all
Is there any way to check if a file is already open by another user before
trying to open it.

If "Filename is open" = true then
abort procedure
else
Workbooks.Open Filename....
end if

Thanks
 
R

RB Smissaert

Try this function:

Function FileIsOpen(strFile As String) As Boolean

Dim hFile As Long

On Error GoTo OpenError

hFile = FreeFile

Open strFile For Input Lock Read As #hFile
Close #hFile

Exit Function
OpenError:

FileIsOpen = Err.Number = 70

End Function


RBS
 
C

Cresta

Thanks
That worked a treat


RB Smissaert said:
Try this function:

Function FileIsOpen(strFile As String) As Boolean

Dim hFile As Long

On Error GoTo OpenError

hFile = FreeFile

Open strFile For Input Lock Read As #hFile
Close #hFile

Exit Function
OpenError:

FileIsOpen = Err.Number = 70

End Function


RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top