I have an Access 2000 database writing to a word document. The issue I am running into is that the database doesn't always open the file but it was coded to always close the file. I need to check first to see if the file is open before I close it. I have located a couple different pieces of code but none seem to do the trick, my guess is that it is vba and the code pieces have found are VB or .net
Code:
Public Function IsFileOpen(FileName As String) As Boolean
Dim iFilenum As Long
Dim iErr As Long
On Error GoTo Errorhandler
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
Errorhandler:
Select Case iErr
Case 0:
IsFileOpen = False
Case 70:
IsFileOpen = True
'add on: close the file
Case Else: Error iErr
End Select
End Function
This is one I found and it seems that when I use it there is no error traping. That is the same as the next code sample.
Code:
Public Function IsFileLocked(strFileName As String) As Boolean
On Error Resume Next
Dim FF As Integer
FF = FreeFile
'An error occurs if the document is currently open.
Open strFileName For Binary Access Read Lock Read As #FF
Close #FF
'Check for Error
If Err.Number Then
Err.Clear
IsFileLocked = True
End If
End Function
The other option I thought of was just checking to see if I could activate the window with that name, but again the error doesn't trap
Code:
Public Function IsOpen(FileName As String) As Boolean
On Error Resume Next
Word.Windows(FileName).Activate
If Err Then
IsOpen = False
Else
IsOpen = True
End If
End Function
So I have decided that I really do not know what I am doing and need a bit of help.
Thanks,