Thursday, August 11, 2005

JMT Forums


An excellent forum location (almost as good as mine!) that has a good selection of ask and answer questions on it.
Feel free to check the questions out and answer them if you can!
Heres an answer I added to the question about checking if files are open in VBA.

Sub checkfileisopen()
filenumber = FreeFile
On Error GoTo accesserror
Open "Q:\filename.xls" For Random Access Read Lock Read Write As #filenumber
GoTo noerror:
accesserror:
MsgBox "The " & selectedfile & " file is currently being modified by another user. This process will terminate.", vbOKOnly + vbExclamation, "File open error"
noerror:
Close #filenumber
End Sub

This code will open a read write lock to the file in question Q:\filename.xls
if the file is already open it will return an error and the error handler will skip it with a message. I use this inside a loop checking all the files in a folder before operating on them