Solved

detection of "fileopen"

Posted on 2000-05-01
35
356 Views
Last Modified: 2012-05-04
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
Comment
Question by:srg1110
  • 15
  • 9
  • 5
  • +2
35 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2766815
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
 
LVL 1

Author Comment

by:srg1110
ID: 2766922
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2766936
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2766956
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2766967
Do you (still) have an ExitFunction before ErrHandler:
0
 

Expert Comment

by:deshmukhn
ID: 2766977
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2766990
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
 

Expert Comment

by:deshmukhn
ID: 2767004
That's right. the permission denied error also is raised when you don't have rename (=move) permission on the file. Thanks angelIII
0
 
LVL 1

Author Comment

by:srg1110
ID: 2767040
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
 
LVL 1

Author Comment

by:srg1110
ID: 2767048
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2767057
Could you post your code between these lines (w/in Main)
On Error GoTo errorhandler

errorhandler:
0
 
LVL 1

Author Comment

by:srg1110
ID: 2767090
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2767101
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
 
LVL 3

Expert Comment

by:Lewy
ID: 2767103
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
 
LVL 1

Author Comment

by:srg1110
ID: 2767214
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2767230
You might replate
IF NOT ERR THEN
by
IF ERR.NUMBER = 0 THEN

whatsoever, did you check my last comment?
0
 
LVL 1

Author Comment

by:srg1110
ID: 2767253
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2767268
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
 
LVL 1

Author Comment

by:srg1110
ID: 2767293
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2767302
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
 
LVL 1

Author Comment

by:srg1110
ID: 2767346
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2767373
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
 
LVL 1

Author Comment

by:srg1110
ID: 2770813
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
 
LVL 3

Expert Comment

by:Lewy
ID: 2770847
Check for any "On Error Resume Next".
I've seen this cause problems even if it's in another routine.
0
 
LVL 1

Author Comment

by:srg1110
ID: 2770951
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2771057
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
 
LVL 1

Author Comment

by:srg1110
ID: 2771076
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
 
LVL 1

Author Comment

by:srg1110
ID: 2771110
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
 
LVL 3

Expert Comment

by:Lewy
ID: 2771143
Are there any mode you can use when you open a file to lock it?
0
 
LVL 1

Author Comment

by:srg1110
ID: 2773810
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
 
LVL 3

Expert Comment

by:Lewy
ID: 2773870
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
 
LVL 1

Author Comment

by:srg1110
ID: 2774172
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
 
LVL 32

Accepted Solution

by:
Erick37 earned 100 total points
ID: 2774321
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
 
LVL 1

Author Comment

by:srg1110
ID: 2774616
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2774674
Glad it helped.
Thanks!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now