But how does it work?
I tried something like this ( I also took err.number = 70 along, since that's the errorcode I'd get)
I'll tell you what I want, I wamt to open a word or wxcel document.
to prevent very bad things I want to make a backup first, i.e. a filecopy command.
Unfortunately any solution I tried (so my guess Is i'done something wrong, but I do not know what -can't find any good examples in VB help)
fails...I tried:
On error GoTo FileError
Filecopy
Exit sub
FileError
If err.number = 55 OR err.number = 70 Then
msgbox("file is in use" )
End If
I don't know if that's all I need or more ... but the way I've been trying to use the error-handling it still doesn't work.
This is what happens:
the program opens a file ... I open the same file again via the program.
the program crashes.
If I put a " ' " in front of the filecopy command line (in debugging)
The error handling nicely records err.numer = 55 or 70
goes to FileError and tells me err 70 has occurred because the file is in use
If I remove the filecopy command word and excel open every next file as a read-only copy. But then: no backup.
So what I am doing wrong results in an inrecoverable error before the program can respond to the error code. Actually I would like something like this:
Private sub OpenExcelSheet()
...
If name & ".xls" = AlreadyOpen then
msgbox("File is already in use Cannot open file")
exit sub
Else
Filecopy name & ".xls" , name & ".bak"
objExcel = New Excel.Application
objSheet = filename Open (filepath & name & ".xls")
etc.
end sub
of course it doesn't matter what it would be like, as long as it works.
From my point of view anything I tried (thanks to everyone that replied, otherwise I would have had anything to go with at all), results in a crash first, because it crashes in " filecopy" instead of seeing the file is in use and either cancelling or skipping the filecopy command.
Quote:
Originally Posted by SpaceFrog
Not sure I am quite understanding your need but here is what I would do to avoid beeing thrown out of my program when attempting to acces already used file :
Code:
onerror goto FileError
'here attempt to open file
FileError:
if errnumber = 55 then
Msgbox "File already open"
end if
onerror goto 0