Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Word, PowerPoint, Outlook, and Other Office Products > File In Use


Reply
 
Thread Tools Display Modes
  #1  
Old 09-03-2003, 06:34 AM
QuantumCat's Avatar
QuantumCat QuantumCat is offline
Senior Contributor
 
Join Date: Aug 2003
Location: Dordrecht,Holland
Posts: 858
Wink File In Use


I have a problem with my VB program
about the program: it's a database that uses Word and Excel as programs -to store extra data or to export lists into easy to handle documents (everybody had word!).

The problem I have is that my VB program can't tell files that are in use from thore that aren't. It returns an error message and exits. I have this problem with my word and excel files, since they are also public files I cannot lock them with VB (the other problem is I don't know how to handle the "close word/exel event" so even if I could lock them, I couldn't unlock them automatically ).

I had hoped that word/excel would give the usual error message 'file in use, would you like to open a copy as read-only'. Wishful thinking Unfortunately.

Can anybody tell me what command/object returns the status of a file is (as in "in-use", like DateLastModified is for the date when last modified.
Reply With Quote
  #2  
Old 09-03-2003, 06:41 AM
QuantumCat's Avatar
QuantumCat QuantumCat is offline
Senior Contributor
 
Join Date: Aug 2003
Location: Dordrecht,Holland
Posts: 858
Default File In Use

I did find something in VSS, but the " manual " wasn't very abundant in telling me how it really worked... It was a DOS command ... great!
Does that mean I can see in CMD that the file is in use just before my program crashes or does it return a value or event I can work with?!

As the visual basic search and help functiosn go -that was all I got, and I'd have to figure out the right keyword(s) as search queries myself -as usual (sigh-it almost make me miss Turbo Pascal, ah those good ald days.... )
Reply With Quote
  #3  
Old 09-03-2003, 07:31 AM
SpaceFrog's Avatar
SpaceFrog SpaceFrog is offline
Contributor
 
Join Date: May 2003
Location: Newt Galaxy
Posts: 542
Default

Couldn't you use an error handler ?
Have your program show a messagebox with errnumber to spot the correct number and avoid the program to throw you out when encountering used file and show instead message box "sorry file in use" ...
__________________
Life is a beach !
Reply With Quote
  #4  
Old 09-03-2003, 07:56 AM
Timbo's Avatar
Timbo Timbo is offline
Green-Eyed

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Bangkok, Thailand
Posts: 10,261
Default

Or use the FileSystemObject to read the property you mentioned:
http://msdn.microsoft.com/library/de...bjectmodel.asp
__________________
"He's not the Messiah. He's a very naughty boy!" - Brian's mum

Can't find the answer? >> Try something new!
Become a Professional
Reply With Quote
  #5  
Old 09-03-2003, 08:20 AM
SpaceFrog's Avatar
SpaceFrog SpaceFrog is offline
Contributor
 
Join Date: May 2003
Location: Newt Galaxy
Posts: 542
Default

Cant find any fso property for file opened or not ...

try with this bit of code :
Code:
if Err.Number = ErrFileAlreadyOpen ' Error 55 strMsg = "This file is already open!."
__________________
Life is a beach !
Reply With Quote
  #6  
Old 09-03-2003, 08:31 AM
SpaceFrog's Avatar
SpaceFrog SpaceFrog is offline
Contributor
 
Join Date: May 2003
Location: Newt Galaxy
Posts: 542
Default

Here is a function given in MSN help that treats standard errors occurring at file openning...

Code:
Function FileErrors () As Integer Dim intMsgType As Integer, strMsg As String Dim intResponse As Integer ' Valeur renvoyée Signification ' 0 Resume ' 1 Resume Next ' 2 Fatal error ' 3 Unknown error intMsgType = vbExclamation Select Case Err.Number Case MnErrDeviceUnavailable ' Erreur 68 strMsg = "This device does not seem available." intMsgType = vbExclamation + 4 Case MnErrDiskNotReady ' Erreur 71 strMsg = "Insert floppy in drive " _ & "and close flap." intmsgtype = vbExclamation + 4 Case MnErrDeviceIO ' Erreur 57 strMsg = "internal disk error" intMsgType = vbExclamation + 4 Case MnErrDiskFull ' Erreur 61 strMsg = "Disk is full, continue ?" intMsgType = vbExclamation + 3 Case ErrBadFileName, ErrBadFileNameOrNumber ' Erreurs 64 & ' 52 strMsg = "Forbidden file name." intMsgType = vbExclamation + 4 Case ErrPathDoesNotExist ' Erreur 76 strMsg = "Ce chemin d'accès n'existe pas." intMsgType = vbExclamation + 4 Case ErrBadFileMode ' Erreur 54 strMsg = "Cannot open file " _ & "in this acces mode." intMsgType = vbExclamation + 4 Case ErrFileAlreadyOpen ' Erreur 55 strMsg = "File already open." intMsgType = vbExclamation + 4 Case ErrInputPastEndOfFile ' Error 62 strMsg = "Non standard End of File " _ strMsg = strMsg & "or attempt to read beyond end of file," strMsg = strMsg & "has taken place " intMsgType = vbExclamation + 4 Case Else FileErrors = 3 Exit Function End Select intResponse = MsgBox (strMsg, intMmsgType, _ "Disk error") Select Case intRresponse Case 1, 4 ' Buttons OK, Retry. FileErrors = 0 Case 5 ' Button Ignore. FileErrors = 1 Case 2, 3 ' Buttons Cancel , End. FileErrors = 2 Case Else FileErrors = 3 End Select End Function
__________________
Life is a beach !
Reply With Quote
  #7  
Old 09-03-2003, 11:50 PM
QuantumCat's Avatar
QuantumCat QuantumCat is offline
Senior Contributor
 
Join Date: Aug 2003
Location: Dordrecht,Holland
Posts: 858
Default

Yeah sure, why not?
My problem is that I have only recently started to use VB. So ANY problem I know for sure VB can handle (either by writing a routine or a VB command of function) I can't solve because I do not know where to look, I don't know the right (or in VB-help's case the EXACT) search queries and keywords and such and such (in this case like "error handler").

It's not that I am asking if VB can do this -it's how it's done or where to find in VB help the least.

But thanks for the reply nonetheless :-)

Quote:
Originally Posted by SpaceFrog
Couldn't you use an error handler ?
Have your program show a messagebox with errnumber to spot the correct number and avoid the program to throw you out when encountering used file and show instead message box "sorry file in use" ...

Reply With Quote
  #8  
Old 09-03-2003, 11:51 PM
QuantumCat's Avatar
QuantumCat QuantumCat is offline
Senior Contributor
 
Join Date: Aug 2003
Location: Dordrecht,Holland
Posts: 858
Default thanks

that goes to you all actually

Thanks for your help :-)
Reply With Quote
  #9  
Old 09-04-2003, 12:45 AM
QuantumCat's Avatar
QuantumCat QuantumCat is offline
Senior Contributor
 
Join Date: Aug 2003
Location: Dordrecht,Holland
Posts: 858
Default okay, what to do next?

I trying to use the error handling tools you've given me
(i also found it in VB-help) but I can't link it to the file that could be in use.

Or do I just have to run the function FileErrors and prevent my program by running along (and give the same error again and again) through an if statement
Reply With Quote
  #10  
Old 09-04-2003, 07:00 AM
SpaceFrog's Avatar
SpaceFrog SpaceFrog is offline
Contributor
 
Join Date: May 2003
Location: Newt Galaxy
Posts: 542
Default

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
__________________
Life is a beach !
Reply With Quote
  #11  
Old 09-04-2003, 03:09 PM
QuantumCat's Avatar
QuantumCat QuantumCat is offline
Senior Contributor
 
Join Date: Aug 2003
Location: Dordrecht,Holland
Posts: 858
Default here's some more information

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

Reply With Quote
  #12  
Old 09-05-2003, 12:13 AM
QuantumCat's Avatar
QuantumCat QuantumCat is offline
Senior Contributor
 
Join Date: Aug 2003
Location: Dordrecht,Holland
Posts: 858
Default

Great it works!
Cool -and thank you (thank you all for your help)
So please forget my previous post, that was a little too premature.
I tried it this morning and it worked great.

Guess I interpreted something wrong (it is very similar to a post in " File In Use pt II")...Like FileError -at first I though it was a function or procedure. And I added the " On Error Goto 0" (which I hadn't yesterday)

So thanks again ;-)



Code:
onerror goto FileError 'here attempt to open file FileError: if errnumber = 55 then Msgbox "File already open" end if onerror goto 0
[/QUOTEPOST]
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Installation Problem - PLs help urgenlty dpdsouza Installation / Documentation 4 12-02-2004 07:09 PM
File Problems: Files Not Closing etc. Templeton Peck General 5 03-29-2003 11:34 PM
Doesn't want to register! MikeyM Installation / Documentation 5 03-02-2003 08:22 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->