Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check state of a file?

Status
Not open for further replies.

simran1

Programmer
Apr 23, 2002
82
US
Hi,

I have this code to open a tif file.
Code:
    If App Is Nothing Then
        Set App = CreateObject("imaging.application")
    End If
    If Img Is Nothing Then
        Set Img = App.createimageviewerobject(1)
    End If
    strPath = File1.Path & "\" & File1.Filename
    Img.Open strPath

Before I open the file using "Img.Open" i would like to know if the file is already open. Because, if the file is open i get a run time error, wwhich i want to avoid.
Please help.

 
Maybe you need to just use an Error handler (On Error Goto) [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Hi,
But is there a way to just check the state of a file, if the file is open or close.
 

Yes, you can try to open the file exclusivly. If you are successful then there is not a lock on it.

[tt]
Option Explicit

Private Sub Form_Load()
If CanOpenFile("c:\z\out1.xls") = True Then
MsgBox "Able to have exclusive rights to file"
Else
MsgBox "File open by another application"
End If
End Sub

Public Function CanOpenFile(PathToFile As String) As Boolean

On Error GoTo CanOpenFileError

Dim FNumb As Integer

If Dir(PathToFile) = "" Then Exit Function

FNumb = FreeFile

Open PathToFile For Input Lock Read Write As #FNumb
Close #FNumb

CanOpenFile = True

Exit Function
CanOpenFileError:

Err.Clear

Close #FNumb

End Function

[/tt]
 
In which case, as CCLINT says, you might as well use an error handler in the original routine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top