Trap VB error in Access

ted.martin

Registered User.
Local time
Today, 15:23
Joined
Sep 24, 2004
Messages
743
I am trying to check whether a Word File is already open before running the Word Object model. VB Code found elsewhere says this should work BUT when the Word file is open, I cannot trap the resulting VB Error 70. It seems that the usual Access Error trapping code only works for Access generated errors; not those generated by VB.

Any thoughts would be appreciated. My code is below:

Public Function IsFileLocked(sFile As String) As Boolean

On Error GoTo Err_FileLocked

'Below is the bit that determines if the file is open.
Open sFile For Binary Access Read Write Lock Read Write As #1
Close #1

Err_FileLocked:
If Err.Number <> 0 Then
MsgBox "E R R O R - The file that your are trying to access," & vbCrLf & "is already open." & vbCrLf & vbCrLf & _
"Please close the file and try again", vbCritical, "IsFileLocked"

IsFileLocked = True
Err.Clear
End If

End Function

Public Sub OpenFile()

On Error GoTo ErrorHandler

If Not IsFileLocked("c:\temp\This is a test.docx") Then
Else
Exit Function
End If

Dim objWord As Word.Application
Dim myDoc As Word.Document

' close any previous links - just housekeeping
Set objWord = Nothing
Set myDoc = Nothing

Set objWord = CreateObject("Word.Application")
Set myDoc = objWord.Documents.Open("c:\temp\This is a test.docx")
objWord.Visible = True

End Sub


+++++++++++++++

I have even tried this code variant but the same VB Error 70 runs and I cannot trap it

Function fIsDocFileOpen(ByVal strDocFileName As String) As Boolean
'**************************************************
'Name: fIsDocFileOpen (Function)
'Purpose: Checks to see if the Word document is already open
'Author: Dev Ashish
'Date: February 11, 1999, 05:50:58 PM
'Called by: Any
'Calls: None
'Inputs: strDocFileName: Full path to the Word document
'Output: True if file is open, false otherwise
'*******************************************
On Error GoTo ErrHandler
Dim intFree As Integer
intFree = FreeFile()
Open strDocFileName For Input Lock Read As intFree
fIsDocFileOpen = False
ExitHere:
On Error Resume Next
Close #intFree
Exit Function
ErrHandler:
fIsDocFileOpen = True
Resume ExitHere
End Function
 
Last edited:
Thanks; yes I have seen that. What I need to know is if a specific Word File is open. The code sample I showed is from the same Access Web source, so not sure why it doesn't work properly.

Error 70 is thrown when the file is in use but the error creates its own Msg Box not mine. All it allows me to do is Close or Debug the programme. I want to exit it properly in the usual way the Error Handler works.

It would be good if uyou could try my code and see if you get the same problem.

thanks for your help.

T
 
Have you tried to compile your code? It has errors.
 
Just complied it and it showed came back with no messages. Did you say you have seen an error? Where? Thanks
 
For starters, Exit Function is not allower in a SubRoutine.
Do you have:
Option Compare Database
Option Explicit
...at the top of your module?
 
Guess which Sub has no ErrorHandler?
Code:
Public Sub OpenFile()

   On Error GoTo ErrorHandler
 
Yes to the two Option statements. The erro handler still does not collect the error. Even took out the Exit Function (which I have used within an IF statment before) and still no joy. This Error 70 still fires but I cannot trap it.
 
The error is occuring in the Function/Sub

Function fIsDocFileOpen(ByVal strDocFileName As String) As Boolean

I have put an Error handler in the other Function; it makes to difference. The code line that errors is this - BUT only when the Function has the value true. This is what it should do as the file is open. When the file is closed, it runs fine as would be expected.

Open strDocFileName For Input Lock Read As intFree - this line of code is just a variant of

Open sFile For Binary Access Read Write Lock Read Write As #1 in the other function which behave excatly the same way.
 
Last edited:
Guess which Sub has no ErrorHandler?
Code:
Public Sub OpenFile()

   On Error GoTo ErrorHandler
The point I was trying to make with this post is the compiler should have caught this. There is NO place for the GoTo to GoTo!!! I suspect you have some corruption involved here. Try importing your db into a fresh db and see if the compiler can catch the error.
 
You may have to comment out the label first to you can get the error back. If it still can't find the error with the goto then try a /decompile. What version of Access are you using?
 
Access 2007 and Vista Business.

I have just copied and pasted the code into a new module; tried it again with no change. I have zipped it for you just in case you fancy giving it a try. i won't be beaten on this. It should work but isn't.

Just run the OpenFile function twice.

The firsat time it is fine and the word doc opens.

Leave it open and run it again. Error 70 happens (good) but the error Handler does not trap it (bad)
 

Attachments

I'll try your file but for the record, there is a big difference between copying code to another module and importing everything into another db. Often times any corruption does not follow the import. We could also be looking at an issue with Vista, which I can not test.
 
Without making any changes and running the function twice I get this dialog box in WinXP.
 

Attachments

  • Error70.jpg
    Error70.jpg
    50.8 KB · Views: 119
Thank you; if you could try that would be good. If it is a Vista or Access 2007 problem; at least we would know.

Just to put the whole thing in context for you, I have a programme that does a Mail Merge with Access data completely from within Access. The programme opens a word template, using Doc.Properties, exports all the required form fields into our word template and then does the MM all at the press of a button. It can add a table too.

All I want to do is trap an error if the user tries to run this MM into a word document that is already open. This code does that except that Error 70 seems to have its own Error Handler rather than mine.

The code I have used is not mine but 2 slightly different versions found on the net when searching "check if word file is open". Both are shown and both have the same Error 70 porblem of bypassing my Error Handler.
 
Here is my Vista/Access 2007 error; I prefer yours if I had a choice!
 

Attachments

  • Error 70.jpg
    Error 70.jpg
    14.3 KB · Views: 110
GOT IT !!!!!!!!! and the answer was on this site:

In VBE, Tools | Options - General tab, ensure Error Trapping is set to "Break on Unhandled Errors".
 

Users who are viewing this thread

Back
Top Bottom