PDA

View Full Version : Check shared file open.



sconly
11-22-2011, 03:43 AM
I have a text file, that is shared over the network, that is being accessed by an Excel solution which in turn is being used by multiple users.

What i am trying to achieve is for the excel solution to check if the text file is being used, by another user, so that it knows whether or not to carry on with whatever it's supposed to do.

In other words, does someone have (or know of) some code that can check whether a shared text file is in use or not?

Thanks.

DAH02
11-22-2011, 09:55 AM
In other words, does someone have (or know of) some code that can check whether a shared text file is in use or not ?
If you want to know if the file is reserved in write exclusively for a user you can used this script:


Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred, file is being queried.
Case Else
Error errnum
End Select
End Function


Cordialy,

DAH02