• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

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...

0
srg1110
Asked:
srg1110
  • 15
  • 9
  • 5
  • +2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
This is because of your MsgBox: they reset the error information (they are calls to another sub, where errors are cleared).

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
0
 
srg1110Author Commented:
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
0
 
Erick37Commented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Erick37Commented:
Or as a function:

Option Explicit


Private Sub Command1_Click()

    Debug.Print IsFileOpen("c:\windows\system\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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Do you (still) have an ExitFunction before ErrHandler:
0
 
deshmukhnCommented:
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.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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.
0
 
deshmukhnCommented:
That's right. the permission denied error also is raised when you don't have rename (=move) permission on the file. Thanks angelIII
0
 
srg1110Author Commented:
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
0
 
srg1110Author Commented:
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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Could you post your code between these lines (w/in Main)
On Error GoTo errorhandler

errorhandler:
0
 
srg1110Author Commented:
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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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 = "" (-;
0
 
LewyCommented:
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
0
 
srg1110Author Commented:
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...
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
You might replate
IF NOT ERR THEN
by
IF ERR.NUMBER = 0 THEN

whatsoever, did you check my last comment?
0
 
srg1110Author Commented:
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.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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?
0
 
srg1110Author Commented:
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?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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.
0
 
srg1110Author Commented:
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...
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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.

0
 
srg1110Author Commented:
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...  
0
 
LewyCommented:
Check for any "On Error Resume Next".
I've seen this cause problems even if it's in another routine.
0
 
srg1110Author Commented:
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.FileSystemObject")


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
0
 
Erick37Commented:
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
0
 
srg1110Author Commented:
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...
0
 
srg1110Author Commented:
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})
0
 
LewyCommented:
Are there any mode you can use when you open a file to lock it?
0
 
srg1110Author Commented:
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?"
0
 
LewyCommented:
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.
0
 
srg1110Author Commented:
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)

0
 
Erick37Commented:
Maybe use ShellExecute to open the doc.  I think if you call it the second time, it will just activate the already opened instance of Word.

"HOWTO: Use ShellExecute to Launch Associated File"

http://support.microsoft.com/support/kb/articles/Q170/9/18.asp

0
 
srg1110Author Commented:
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...


                         
0
 
Erick37Commented:
Glad it helped.
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 15
  • 9
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now