Results 1 to 3 of 3
  1. #1
    ralph_davies
    Guest

    Detecting if a file is open (Excel97/2000)

    I am using a Log to store data which is generated by users
    The Log (Workbook) is opened, data is written and then the Log is closed (a brief opperation).
    My users are on a network and there is a possibility that the program will try to open the Log when another user has it open (ie Duplicate copies of the code can be exicuting at the same time)
    I need some way to detect in the VB code if the Log is open then loop till it closes then open it so I avoide clashes.
    I have had a look at previous postings but I can't seem to find the answer.
    Can anyone Help

    Thanks

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detecting if a file is open (Excel97/2000)

    I don't have a network available, so I could not test this, but I think that something like this should work. It will need some more testing put in to make sure it does not loop forever if some other error (like the file not being there to open) does not cause the routine to loop forever.

    <pre>Dim oWB As Workbook
    Application.DisplayAlerts = False
    While oWB Is Nothing
    Set oWB = Workbooks.Open("C:LogdirLog.xls")
    Wend
    Application.DisplayAlerts = True
    </pre>


    After a little more thought, I think that you will also have to use On Error to trap the error that occurs when the shared log file is open. You should be able to check the error value to determine if the error was caused by the file being open or something else. Maybe something like this where nnnn is the error number for the file being open:

    <pre>Dim oWB As Workbook
    Application.DisplayAlerts = False
    While oWB Is Nothing
    On Error Resume Next
    oWB = Workbooks.Open("C:LogdirLog.xls")
    On Error GoTo 0
    If Err.Number <> nnnn Then Exit Sub
    Wend
    Application.DisplayAlerts = True
    </pre>

    Legare Coleman

  4. #3
    Bronze Lounger
    Join Date
    Jun 2001
    Location
    New York, New York, Lebanon
    Posts
    1,437
    Thanks
    1
    Thanked 1 Time in 1 Post

    Re: Detecting if a file is open (Excel97/2000)

    Ralph

    In addition to Legare comments, I think you should look into sharing the workbook.

    From the Tools menu choose Share Workbook...

    If the code you are running is in multiple workbooks on multiple PCs, then sharing the Log.xls workbook will allow you to write to it without having to worry about if anyone else is writing to it at that time. It will accept the changes from all the users who are working with it, and you can have some neat history to see who did what, and you can accept these changes or reject them, if that is part of your process.

    Wassim
    <img src=/S/compute.gif border=0 alt=compute width=40 height=20> in the <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

Posting Permissions

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