Results 1 to 5 of 5

Checking to see if a file has been opened in Read/Write or Read Only

Threaded View

  1. #1
    Valued Forum Contributor
    Join Date
    10-13-2010
    Location
    Sunderland, England
    MS-Off Ver
    Excel 2007 (Home) / 2003 (Work)
    Posts
    740

    Checking to see if a file has been opened in Read/Write or Read Only

    Hi guys

    I feel like this should be simple but I don't seem to be able to work out the answer.

    Currently I have a sheet containing data that is used by around 50 users. They each have their own allocator sheet, which (when a macro runs) then opens up the master sheet, takes some data, then saves and closes the master sheet again. This way there are no sharing violations, etc, and each item is only allocated to one person.

    In order for this to work, the allocator sheet checks to see if the master sheet is in use before opening it, using the following:

    Function FileLocked(strFileName As String) As Boolean
       On Error Resume Next
       Open strFileName For Binary Access Read Write Lock Read Write As #1
       Close #1
       If Err.Number <> 0 Then
          FileLocked = True
          Err.Clear
       End If
    End Function
    and then in the main code:

    ChDir "C:\whatever\etc\"
    strFileName = "C:\whatever\etc\mainsheet.xls"
    x = 2
    Do While x <> 1
    On Error Resume Next
    
    If Not FileLocked(strFileName) Then
          Workbooks.Open strFileName, , , , , "abc"
          x = 1
    Else
    x = x + 1
    If x > 6 Then
    MsgBox "DOWNLOAD FAILED - File In Use, Please Try Again."
    Exit Sub
    End If
    End If
    Loop
    This works fine 99% of the time, but there have been a small number of incidents where people have both downloaded the same items. I believe this is because the file takes a small amount of time to open, and isn't opened until after the check is done. So if 2 people run the macro almost simultaneously, the second person's sees that the file isn't in use, and starts to open it - but the first person's has already opened the file by this stage.

    So what I'd like to do is check (once the file has been opened for use) to see if the file is only opened as 'read-only' state, and if so to repeat the process again.

    Any ideas guys?
    Last edited by brokenbiscuits; 01-13-2014 at 09:17 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 06-26-2012, 02:00 PM
  2. Skip The Read Only Prompt When I Open A File - Excel, Open it read-write instead.
    By harry_1805 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-06-2012, 02:14 AM
  3. stop excel file opened as read only if already opened by another u
    By bobm in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-09-2011, 08:45 AM
  4. I have a read only xl file, I need it to be read and write
    By drama queen in forum Excel General
    Replies: 3
    Last Post: 06-30-2006, 07:30 PM
  5. How can a file be converted from Read-Only to Read/Write
    By Jim in Apopka in forum Excel General
    Replies: 2
    Last Post: 11-19-2005, 01:00 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1