Consulting

Results 1 to 2 of 2

Thread: Check shared file open.

  1. #1
    VBAX Regular
    Joined
    Apr 2010
    Posts
    32
    Location

    Question Check shared file open.

    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.

  2. #2
    VBAX Newbie
    Joined
    Nov 2011
    Location
    Near Reims in France
    Posts
    4
    Location
    Quote Originally Posted by sconly
    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:

    [vba]
    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
    [/vba]

    Cordialy,

    DAH02

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •