srg1110
asked on
detection of "fileopen"
I'm currently writing a program which creates a number of MSWORD 97 documents. this program also has the ability to access these documents over an area wide network. my problem is that I need to figure out how to write a "fileopen" statement to check whether the file that a user wants to access isn't being used by another user, and thus give a "file already in use" msg. One way I've managed to do this is to trap the error of a fileopen, which is err#55... however for some reason this is not working anymore. instead I get err#_ and after I tries having a description and # of the err sent to me: it sent me err#0 and a blank description. it's not even detecting the openfile...
It seems to be happening right after I call the main procedure after a separate one. This is the code that I have that seems to be the problem:
__________________________ __________ ____
Private Sub com1_Click()
Y = Frame2
X = T
Z = Frame3
MAIN
End Sub
__________________________ __________ ____
and this is the main function that is giving me the hassles:
__________________________ __________ ____
'(w/in the main function)
On Error GoTo errorhandler
errorhandler:
MsgBox (Err.Description)
MsgBox (Err.Number)
If Err.Number = 55 Then
MsgBox ("file in use")
End
End If
On Error Resume Next
__________________________ __________ ____
if anyone can help me I'd be very appreciative.. thanks...
It seems to be happening right after I call the main procedure after a separate one. This is the code that I have that seems to be the problem:
__________________________
Private Sub com1_Click()
Y = Frame2
X = T
Z = Frame3
MAIN
End Sub
__________________________
and this is the main function that is giving me the hassles:
__________________________
'(w/in the main function)
On Error GoTo errorhandler
errorhandler:
MsgBox (Err.Description)
MsgBox (Err.Number)
If Err.Number = 55 Then
MsgBox ("file in use")
End
End If
On Error Resume Next
__________________________
if anyone can help me I'd be very appreciative.. thanks...
ASKER
OK, this didn't work... it's still telling me that I have err#0 and there is no description of the err. also for some reason it going to the err handler when there isn't supposed to be an err... when there are no files open it is still going to the err handler and I can't figure out why it's doing this... I have a feeling that it's because the err is being caused in the previous function {Private Sub com1_Click() }where the main function is called and what I've found out is that if you call an error handler in different function from the err then the err turns to "0" and I can't figure out what to do about this cause I don't know where the err is or why it's happening (also I've heard of a way of "pushing" and "popping" an err... but don't know much about this.---please advise
Private Sub Command1_Click()
On Error GoTo ERRHANDLER
Open "c:\windows\system\user32. dll" For Output As #1
Close #1
'Make sure you Exit Sub
Exit Sub
ERRHANDLER:
MsgBox Err.Description, , "Error " & Err.Number
''End?
End Sub
On Error GoTo ERRHANDLER
Open "c:\windows\system\user32.
Close #1
'Make sure you Exit Sub
Exit Sub
ERRHANDLER:
MsgBox Err.Description, , "Error " & Err.Number
''End?
End Sub
Or as a function:
Option Explicit
Private Sub Command1_Click()
Debug.Print IsFileOpen("c:\windows\sys tem\user32 .dll")
End Sub
Public Function IsFileOpen(ByVal sFile As String) As Boolean
On Error GoTo ERRHANDLER
Open sFile For Output As #1
Close #1
IsFileOpen = False
'Make sure you Exit Sub
Exit Function
ERRHANDLER:
MsgBox sFile & vbCrLf & Err.Description, , "Error " & Err.Number
IsFileOpen = True
End Function
Option Explicit
Private Sub Command1_Click()
Debug.Print IsFileOpen("c:\windows\sys
End Sub
Public Function IsFileOpen(ByVal sFile As String) As Boolean
On Error GoTo ERRHANDLER
Open sFile For Output As #1
Close #1
IsFileOpen = False
'Make sure you Exit Sub
Exit Function
ERRHANDLER:
MsgBox sFile & vbCrLf & Err.Description, , "Error " & Err.Number
IsFileOpen = True
End Function
Do you (still) have an ExitFunction before ErrHandler:
try Renaming the file with the same name and the
trap the permission denied error. If the file is open, you will get the permission denied error otherwise the file will be renamed by the same name.
trap the permission denied error. If the file is open, you will get the permission denied error otherwise the file will be renamed by the same name.
deshmukhn:
what a bad suggestion, and even as an answer. Shame on you (-;
Ok, you are new to EE and excused this time.
FYI: the permission denied error also is raised when you don't have rename (=move) permission on the file.
what a bad suggestion, and even as an answer. Shame on you (-;
Ok, you are new to EE and excused this time.
FYI: the permission denied error also is raised when you don't have rename (=move) permission on the file.
That's right. the permission denied error also is raised when you don't have rename (=move) permission on the file. Thanks angelIII
ASKER
I am trapping the permission denied error, but this is confusing me even more than I thought I could be. what do I need the permission denied statment for? if it's a test as to whether my err handling works, yes the problem is that once I call the main function I get err#0 and I need err#55
ASKER
I am trapping the permission denied error, but this is confusing me even more than I thought I could be. what do I need the permission denied statment for? if it's a test as to whether my err handling works, yes the problem is that once I call the main function I get err#0 and I need err#55
Could you post your code between these lines (w/in Main)
On Error GoTo errorhandler
errorhandler:
On Error GoTo errorhandler
errorhandler:
ASKER
On Error GoTo errorhandler
Set Wordbasic = CreateObject("Word.Basic")
Wordbasic.ChDefaultDir "c:\windows\desktop\temp\" , 0 '<- temporary code for debugging
bigdate = Format(Date, "mm/dd/yy") '<- assign the date to a certain format
newdate = Format(Date, "mmdd") '<- assigning new date to a specific format
If (Wordbasic.[files$](X & newdate & V & ".DOC") <> "") Then '<- check for the fax
errorhandler:
Dim strErr As String
Dim lngErr As Long
strErr = Err.Description
lngErr = Err.Number
MsgBox strErr
MsgBox lngErr
If lngErr = 55 Then
MsgBox ("file in use")
End
End If
On Error Resume Next
Set Wordbasic = CreateObject("Word.Basic")
Wordbasic.ChDefaultDir "c:\windows\desktop\temp\"
bigdate = Format(Date, "mm/dd/yy") '<- assign the date to a certain format
newdate = Format(Date, "mmdd") '<- assigning new date to a specific format
If (Wordbasic.[files$](X & newdate & V & ".DOC") <> "") Then '<- check for the fax
errorhandler:
Dim strErr As String
Dim lngErr As Long
strErr = Err.Description
lngErr = Err.Number
MsgBox strErr
MsgBox lngErr
If lngErr = 55 Then
MsgBox ("file in use")
End
End If
On Error Resume Next
As i said (see some comment above):
you have no Exit Function before the errorhandler:, so even if you don't get an error, you enter the errorhandler code, with err.number = 0 and err.description = "" (-;
you have no Exit Function before the errorhandler:, so even if you don't get an error, you enter the errorhandler code, with err.number = 0 and err.description = "" (-;
Make sure the File number is not in use>
Do local Error Handling as shown below. The On Error Resume Next will clear the Err.
Try:
Dim File1 As Integer
On Error Resume Next
File1 = FreeFile
Open "c:\windows\system\user32. dll" For Output As #File1
If Not Err Then
Close #File1
Else
MsgBox Err.Description, , "Error " & Err.Number
End If
On Error GoTo 0
Do local Error Handling as shown below. The On Error Resume Next will clear the Err.
Try:
Dim File1 As Integer
On Error Resume Next
File1 = FreeFile
Open "c:\windows\system\user32.
If Not Err Then
Close #File1
Else
MsgBox Err.Description, , "Error " & Err.Number
End If
On Error GoTo 0
ASKER
Lewy:
I did exactly what you said and placed the code in there, and there was no err. however I even replaced the file user32.dll with the file I was origionally testing with and there was no error. and that was alright. the funny part was when I already had the file open and then ran the code (no err#55) I got a clean run...
I did exactly what you said and placed the code in there, and there was no err. however I even replaced the file user32.dll with the file I was origionally testing with and there was no error. and that was alright. the funny part was when I already had the file open and then ran the code (no err#55) I got a clean run...
You might replate
IF NOT ERR THEN
by
IF ERR.NUMBER = 0 THEN
whatsoever, did you check my last comment?
IF NOT ERR THEN
by
IF ERR.NUMBER = 0 THEN
whatsoever, did you check my last comment?
ASKER
angelIII: did exactly what you said. it's still giving me no err, and is turning true to "if err.number = 0" and even if the file is already open and I try openning the same one, it's doing the same thing.
If your code is still the same (+EXIT FUNCTION) as the one you posted for my request, it's normal, because no file open request in code?
ASKER
so how is this helping me? you think that the reason this is doing what it'ts doing is because I'm calling the Main procedure and I'm not supposed to do that?
What I think is that you expect error #55 occuring, because you try to open a file that is already open, but you actually do not seem to call any code trying to do that (opening a file) so you can't get that error.
For example, do you have (copied) Erick37's function IsFileOpen AND do you call that function somewhere in your Main function. If the answer is no, then do so, and report if the error still does not occur.
For example, do you have (copied) Erick37's function IsFileOpen AND do you call that function somewhere in your Main function. If the answer is no, then do so, and report if the error still does not occur.
ASKER
ok, I have Erick37's code in my program and I am running it and it's now giving me the permission denied error saying that it's err#70... so now what? I'm still confused...
If you don't know, there are millions and millions of errors, some of them sharing the same number, some errors being with several numbers (as error may be only similar).
So you need to
1) Change your IF statement like this
IF lngErr = 55 or lngErr = 70 Then
IsFileOpen = true
End If
2) Check if any other error message could be raised during Open statement
You might consider that the file is open on any error.
So you need to
1) Change your IF statement like this
IF lngErr = 55 or lngErr = 70 Then
IsFileOpen = true
End If
2) Check if any other error message could be raised during Open statement
You might consider that the file is open on any error.
ASKER
OK, heres' the deal, now I have managed to solve the problem of the error#0. I listened to what AngelIII had to say and placed the errhandler after the exit sub and i wasn't getting err#0 anymore. HOWEVER!! I'm not getting any errors... the file is already open and i have checked that, and it's still not registering there being any err... it's just bypassing the err handler completely and not doing anything... I'm starting to doubt my ability to reason in logic...
Check for any "On Error Resume Next".
I've seen this cause problems even if it's in another routine.
I've seen this cause problems even if it's in another routine.
ASKER
I checked, there are none... I don't know why it's not giving me the error... I have a copy of the file (word doc) open and I'm openning it using my program and it's not giving me any errors at all...
HERE'S THE CODE:
set wordbasic = CreateObject("Scripting.Fi leSystemOb ject")
Wordbasic.ChDefaultDir "c:\windows\desktop\temp\" , 0 '<~open directory
bigdate = Format(Date, "mm/dd/yy") newdate = Format(Date, "mmdd")
If (Wordbasic.[files$](X & newdate & V & ".DOC") <> "") Then '<~ check for the fax
On Error GoTo errorhandler
Wordbasic.appshow '<~ opens word
Wordbasic.AppMaximize '<~ maximizes word in window
Wordbasic.fileopen (X & newdate & V & ".DOC") '<~ open the fax
'End If
Else
'V~this is the msgbox to ask for a new creation of a fax
Wordbasic.MsgBox "File not found, Creating a new one."
Wordbasic.appshow '<~ statement opens word
Wordbasic.AppMaximize '<~ statement maximizes word in the window
newname = (X & newdate)
NewOne
End If
Exit Sub
errorhandler:
If Err.Number = 55 Then
MsgBox ("file in use")
End
End If
If Err.Number = 0 Then
MsgBox ("error 0")
End If
HERE'S THE CODE:
set wordbasic = CreateObject("Scripting.Fi
Wordbasic.ChDefaultDir "c:\windows\desktop\temp\"
bigdate = Format(Date, "mm/dd/yy") newdate = Format(Date, "mmdd")
If (Wordbasic.[files$](X & newdate & V & ".DOC") <> "") Then '<~ check for the fax
On Error GoTo errorhandler
Wordbasic.appshow '<~ opens word
Wordbasic.AppMaximize '<~ maximizes word in window
Wordbasic.fileopen (X & newdate & V & ".DOC") '<~ open the fax
'End If
Else
'V~this is the msgbox to ask for a new creation of a fax
Wordbasic.MsgBox "File not found, Creating a new one."
Wordbasic.appshow '<~ statement opens word
Wordbasic.AppMaximize '<~ statement maximizes word in the window
newname = (X & newdate)
NewOne
End If
Exit Sub
errorhandler:
If Err.Number = 55 Then
MsgBox ("file in use")
End
End If
If Err.Number = 0 Then
MsgBox ("error 0")
End If
Try this:
errorhandler:
Select Case Err.Number
Case 55, 70
MsgBox "file in use"
Case Else
MsgBox Err.Description
End Select
Set WordBasic = Nothing
Unload Me
End Sub
errorhandler:
Select Case Err.Number
Case 55, 70
MsgBox "file in use"
Case Else
MsgBox Err.Description
End Select
Set WordBasic = Nothing
Unload Me
End Sub
ASKER
nope, nothing... I can't seem to get it to recognize that there is an open file... it is actually openning a new instance of the file in word when there is on open already... and it just can't see it...
ASKER
what it seems like it's doing is just not realising that the file is already open. VB is treating the file as though it's not open when it is (I've tried it with 7 instances of it file open at once and it still opened an 8th w/o batting an eye...{if it had eyes})
Are there any mode you can use when you open a file to lock it?
ASKER
what you mean by mode?
I'm trying to figure out how windows or office flags a file once it's open... is there some sort of change in the properties or attributes of a file if it already open, as opposed to if it is closed? if so could I use this flag to test for "fileopen?"
I'm trying to figure out how windows or office flags a file once it's open... is there some sort of change in the properties or attributes of a file if it already open, as opposed to if it is closed? if so could I use this flag to test for "fileopen?"
I was referencing the File Modes.
From VB Help
This code example opens the file in Binary mode for reading; other processes can't read file.
Open "TESTFILE" For Binary Access Read Lock Read As #1
I've never worked with the Access or Lock Key words. I can't find any additional information. You may have to experiment to find what works for your situation.
From VB Help
This code example opens the file in Binary mode for reading; other processes can't read file.
Open "TESTFILE" For Binary Access Read Lock Read As #1
I've never worked with the Access or Lock Key words. I can't find any additional information. You may have to experiment to find what works for your situation.
ASKER
I'll see what I can do...
On another note, is there any other way to check whether a file is already in use? if so then I could just use that instead...
and on another note. perhaps the reason it's not working is because I'm using wordbasic commands. what if I use normal VB commands instead of Wordbasic commands to open the file... the problem however is that I can't find anywhere how to set a directory and open the file in a specified directory for use in word (and open word too)
On another note, is there any other way to check whether a file is already in use? if so then I could just use that instead...
and on another note. perhaps the reason it's not working is because I'm using wordbasic commands. what if I use normal VB commands instead of Wordbasic commands to open the file... the problem however is that I can't find anywhere how to set a directory and open the file in a specified directory for use in word (and open word too)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
THANKS!!!!
I've been working on this for the past week and a half and nothing as far as errhandling seemed to be working. In using the shell execute function it actually uses MS word's error checking to check the document for me!! thanks alot man... good on'ya...
I've been working on this for the past week and a half and nothing as far as errhandling seemed to be working. In using the shell execute function it actually uses MS word's error checking to check the document for me!! thanks alot man... good on'ya...
Glad it helped.
Thanks!
Thanks!
You may use this:
errorhandler:
dim strErr as string
dim lngErr as Long
strErr = Err.Description
lngErr = Err.Number
MsgBox strErr
MsgBox lngErr
If lngErr = 55 Then
MsgBox ("file in use")
End
End If