ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Check if a file is already open (https://www.excelbanter.com/excel-programming/426340-check-if-file-already-open.html)

Cresta

Check if a file is already open
 
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

RB Smissaert

Check if a file is already open
 
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


"Cresta" wrote in message
...
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



Cresta

Check if a file is already open
 
Thanks
That worked a treat


"RB Smissaert" wrote:

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


"Cresta" wrote in message
...
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





All times are GMT +1. The time now is 12:39 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com