Check the opened file

almubarmeg
05-12-2003, 06:22 PM
Hello guys'

I would like to know how to check whether a file is already open or not
I tried to use this code but it didn't work ,,any help



the code :

WiPrivate Sub lbopen_Click()

with dlgCommonDialog
.DialogTitle = "Open"
.CancelError = False
.Filter = " (*.txt)|*.txt"
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If

ofile = .FileName

On Error GoTo Errorhandle

Open ofile For Input As #numberf


ok:
Exit Sub

Errorhandle:
MsgBox " The file is already open "

Resume ok


End With

OnErr0r
05-12-2003, 07:41 PM
Hello guys'

I would like to know how to check whether a file is already open or not
I tried to use this code but it didn't work ,,any help



the code :

WiPrivate Sub lbopen_Click()

with dlgCommonDialog
.DialogTitle = "Open"
.CancelError = False
.Filter = " (*.txt)|*.txt"
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If

ofile = .FileName

On Error GoTo Errorhandle

Open ofile For Input As #numberf


ok:
Exit Sub

Errorhandle:
MsgBox " The file is already open "

Resume ok


End With

To check if you've closed the file, do this when you close, then check the value of your numberf variable. (assuming its public or has form wide scope).

Close numberf
numberf = 0

Later check if numberf = 0.

If you're talking about trapping errors from some other application having the file opened and locked, then the error trap should work.

Code should look something like this:


Private Sub lbopen_Click()

With dlgCommonDialog
.DialogTitle = "Open"
.CancelError = False
.Filter = " (*.txt)|*.txt"
.ShowOpen

ofile = .FileName
End With

On Error GoTo Errorhandle

Open ofile For Input As #numberf

' code to read from the file
' Close numberf ?

Exit Sub

Errorhandle:
MsgBox " The file is already open "

End Sub

mohanakannan
05-12-2003, 10:45 PM
hi..try this code..
but i am not the author.. :cool:


Public Function isFileOpen(ByVal strDocFile As String) As Boolean
On Error GoTo ErrHandler
Dim intFree As Integer
intFree = FreeFile()
Open strDocFile For Input Lock Read As intFree
isFileOpen = False
ExitHere:
On Error Resume Next
Close #intFree
Exit Function
ErrHandler:
isFileOpen = True
Resume ExitHere
End Function


this function will return tru if the file is open else false.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum