Solved

In Access vba detect whether an excel file is open

Posted on 2011-09-21
3
303 Views
Last Modified: 2012-05-12
I want to read/update information off an excel spreadsheet but don't want to do this while someone has it open. You cannot delete a file when someone has it open so system must record somewhere? - is there a way of finding out if a file is opened by someone - this file will be on a network drive. I looked on the FileSystemObject but could not see anything obvious to tell me.
0
Comment
Question by:donhannam
3 Comments
 
LVL 12

Expert Comment

by:danishani
ID: 36577015
Check below thread out of Ron de Bruin, scroll down, and you find a nice solution:
http://www.rondebruin.nl/exist.htm

Or this thread:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=468

Hope this helps,
Daniel
0
 
LVL 20

Accepted Solution

by:
GrahamMandeno earned 500 total points
ID: 36577107
Hi Don

Try this function:
Public Function FileIsLocked(strFileName As String) As Boolean
Dim hFile As Long
  On Error Resume Next
  hFile = FreeFile
  Open strFileName For Input Lock Read Write As #hFile
  Select Case Err.Number
    Case 0
      Close #hFile
      FileIsLocked = False
    Case 70
      FileIsLocked = True
    Case Else
      MsgBox "Error opening " & strFileName & vbCrLf & Err.Description, vbExclamation
      FileIsLocked = True
  End Select
  Err.Clear
End Function

Open in new window


It simply attempts to open the file for exclusive read access and checks the failure status.

Best regards,
Graham Mandeno [Access MVP 1996-2011]
0
 

Author Closing Comment

by:donhannam
ID: 36578372
Thanks Graham works perfectly

danishani: Think the links you had effectively do the same thing but prefered the one Graham provided.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question